2 * Copyright (c) 2023 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>
39 std::ostream& operator<<(std::ostream& o, const Graphics::BufferCreateInfo& bufferCreateInfo)
41 return o << "usage:" << std::hex << bufferCreateInfo.usage << ", size:" << std::dec << bufferCreateInfo.size;
44 std::ostream& operator<<(std::ostream& o, const Graphics::CommandBufferCreateInfo& commandBufferCreateInfo)
46 return o << "level:" << (commandBufferCreateInfo.level == Graphics::CommandBufferLevel::PRIMARY ? "PRIMARY" : "SECONDARY")
47 << ", fixedCapacity:" << std::dec << commandBufferCreateInfo.fixedCapacity;
50 std::ostream& operator<<(std::ostream& o, const Graphics::TextureType& textureType)
54 case Graphics::TextureType::TEXTURE_2D:
57 case Graphics::TextureType::TEXTURE_3D:
60 case Graphics::TextureType::TEXTURE_CUBEMAP:
61 o << "TEXTURE_CUBEMAP";
67 std::ostream& operator<<(std::ostream& o, const Graphics::Extent2D extent)
69 o << "width:" << extent.width << ", height:" << extent.height;
73 std::ostream& operator<<(std::ostream& o, const Graphics::TextureCreateInfo& createInfo)
75 o << "textureType:" << createInfo.textureType
76 << " size:" << createInfo.size
77 << " format:" << static_cast<uint32_t>(createInfo.format)
78 << " mipMapFlag:" << createInfo.mipMapFlag
79 << " layout:" << (createInfo.layout == Graphics::TextureLayout::LINEAR ? "LINEAR" : "OPTIMAL")
80 << " usageFlags:" << std::hex << createInfo.usageFlags
81 << " data:" << std::hex << createInfo.data
82 << " dataSize:" << std::dec << createInfo.dataSize
83 << " nativeImagePtr:" << std::hex << createInfo.nativeImagePtr.Get();
87 std::ostream& operator<<(std::ostream& o, Graphics::SamplerAddressMode addressMode)
91 case Graphics::SamplerAddressMode::REPEAT:
94 case Graphics::SamplerAddressMode::MIRRORED_REPEAT:
95 o << "MIRRORED_REPEAT";
97 case Graphics::SamplerAddressMode::CLAMP_TO_EDGE:
100 case Graphics::SamplerAddressMode::CLAMP_TO_BORDER:
101 o << "CLAMP_TO_BORDER";
103 case Graphics::SamplerAddressMode::MIRROR_CLAMP_TO_EDGE:
104 o << "MIRROR_CLAMP_TO_EDGE";
110 std::ostream& operator<<(std::ostream& o, Graphics::SamplerFilter filterMode)
114 case Graphics::SamplerFilter::LINEAR:
117 case Graphics::SamplerFilter::NEAREST:
124 std::ostream& operator<<(std::ostream& o, Graphics::SamplerMipmapMode mipmapMode)
128 case Graphics::SamplerMipmapMode::NONE:
131 case Graphics::SamplerMipmapMode::LINEAR:
134 case Graphics::SamplerMipmapMode::NEAREST:
141 std::ostream& operator<<(std::ostream& o, const Graphics::SamplerCreateInfo& createInfo)
143 o << "minFilter:" << createInfo.minFilter
144 << " magFilter:" << createInfo.magFilter
145 << " wrapModeU:" << createInfo.addressModeU
146 << " wrapModeV:" << createInfo.addressModeV
147 << " wrapModeW:" << createInfo.addressModeW
148 << " mipMapMode:" << createInfo.mipMapMode;
152 std::ostream& operator<<(std::ostream& o, const Graphics::ColorAttachment& colorAttachment)
154 o << "attachmentId:" << colorAttachment.attachmentId
155 << " layerId:" << colorAttachment.layerId
156 << " levelId:" << colorAttachment.levelId
157 << " texture:" << colorAttachment.texture;
161 std::ostream& operator<<(std::ostream& o, const Graphics::DepthStencilAttachment& depthStencilAttachment)
163 o << "depthTexture:" << depthStencilAttachment.depthTexture
164 << "depthLevel:" << depthStencilAttachment.depthLevel
165 << "stencilTexture:" << depthStencilAttachment.stencilTexture
166 << "stencilLevel:" << depthStencilAttachment.stencilLevel;
170 std::ostream& operator<<(std::ostream& o, const Graphics::FramebufferCreateInfo& createInfo)
172 o << "colorAttachments:";
173 for(auto i = 0u; i < createInfo.colorAttachments.size(); ++i)
175 o << "[" << i << "]=" << createInfo.colorAttachments[i] << " ";
177 o << "depthStencilAttachment:" << createInfo.depthStencilAttachment;
178 o << "size: " << createInfo.size;
182 int GetNumComponents(Graphics::VertexInputFormat vertexFormat)
186 case Graphics::VertexInputFormat::UNDEFINED:
187 case Graphics::VertexInputFormat::FLOAT:
188 case Graphics::VertexInputFormat::INTEGER:
190 case Graphics::VertexInputFormat::IVECTOR2:
191 case Graphics::VertexInputFormat::FVECTOR2:
193 case Graphics::VertexInputFormat::IVECTOR3:
194 case Graphics::VertexInputFormat::FVECTOR3:
196 case Graphics::VertexInputFormat::FVECTOR4:
197 case Graphics::VertexInputFormat::IVECTOR4:
203 GLint GetSize(Graphics::VertexInputFormat vertexFormat)
207 case Graphics::VertexInputFormat::UNDEFINED:
209 case Graphics::VertexInputFormat::INTEGER:
210 case Graphics::VertexInputFormat::IVECTOR2:
211 case Graphics::VertexInputFormat::IVECTOR3:
212 case Graphics::VertexInputFormat::IVECTOR4:
214 case Graphics::VertexInputFormat::FLOAT:
215 case Graphics::VertexInputFormat::FVECTOR2:
216 case Graphics::VertexInputFormat::FVECTOR3:
217 case Graphics::VertexInputFormat::FVECTOR4:
223 GLint GetGlType(Graphics::VertexInputFormat vertexFormat)
227 case Graphics::VertexInputFormat::UNDEFINED:
229 case Graphics::VertexInputFormat::INTEGER:
230 case Graphics::VertexInputFormat::IVECTOR2:
231 case Graphics::VertexInputFormat::IVECTOR3:
232 case Graphics::VertexInputFormat::IVECTOR4:
234 case Graphics::VertexInputFormat::FLOAT:
235 case Graphics::VertexInputFormat::FVECTOR2:
236 case Graphics::VertexInputFormat::FVECTOR3:
237 case Graphics::VertexInputFormat::FVECTOR4:
243 GLenum GetTopology(Graphics::PrimitiveTopology topology)
247 case Graphics::PrimitiveTopology::POINT_LIST:
250 case Graphics::PrimitiveTopology::LINE_LIST:
253 case Graphics::PrimitiveTopology::LINE_LOOP:
256 case Graphics::PrimitiveTopology::LINE_STRIP:
257 return GL_LINE_STRIP;
259 case Graphics::PrimitiveTopology::TRIANGLE_LIST:
262 case Graphics::PrimitiveTopology::TRIANGLE_STRIP:
263 return GL_TRIANGLE_STRIP;
265 case Graphics::PrimitiveTopology::TRIANGLE_FAN:
266 return GL_TRIANGLE_FAN;
271 GLenum GetCullFace(Graphics::CullMode cullMode)
275 case Graphics::CullMode::NONE:
277 case Graphics::CullMode::FRONT:
279 case Graphics::CullMode::BACK:
281 case Graphics::CullMode::FRONT_AND_BACK:
282 return GL_FRONT_AND_BACK;
287 GLenum GetFrontFace(Graphics::FrontFace frontFace)
289 if(frontFace == Graphics::FrontFace::CLOCKWISE)
296 GLenum GetBlendFactor(Graphics::BlendFactor blendFactor)
298 GLenum glFactor = GL_ZERO;
302 case Graphics::BlendFactor::ZERO:
305 case Graphics::BlendFactor::ONE:
308 case Graphics::BlendFactor::SRC_COLOR:
309 glFactor = GL_SRC_COLOR;
311 case Graphics::BlendFactor::ONE_MINUS_SRC_COLOR:
312 glFactor = GL_ONE_MINUS_SRC_COLOR;
314 case Graphics::BlendFactor::DST_COLOR:
315 glFactor = GL_DST_COLOR;
317 case Graphics::BlendFactor::ONE_MINUS_DST_COLOR:
318 glFactor = GL_ONE_MINUS_DST_COLOR;
320 case Graphics::BlendFactor::SRC_ALPHA:
321 glFactor = GL_SRC_ALPHA;
323 case Graphics::BlendFactor::ONE_MINUS_SRC_ALPHA:
324 glFactor = GL_ONE_MINUS_SRC_ALPHA;
326 case Graphics::BlendFactor::DST_ALPHA:
327 glFactor = GL_DST_ALPHA;
329 case Graphics::BlendFactor::ONE_MINUS_DST_ALPHA:
330 glFactor = GL_ONE_MINUS_DST_ALPHA;
332 case Graphics::BlendFactor::CONSTANT_COLOR:
333 glFactor = GL_CONSTANT_COLOR;
335 case Graphics::BlendFactor::ONE_MINUS_CONSTANT_COLOR:
336 glFactor = GL_ONE_MINUS_CONSTANT_COLOR;
338 case Graphics::BlendFactor::CONSTANT_ALPHA:
339 glFactor = GL_CONSTANT_ALPHA;
341 case Graphics::BlendFactor::ONE_MINUS_CONSTANT_ALPHA:
342 glFactor = GL_ONE_MINUS_CONSTANT_ALPHA;
344 case Graphics::BlendFactor::SRC_ALPHA_SATURATE:
345 glFactor = GL_SRC_ALPHA_SATURATE;
347 // GLES doesn't appear to have dual source blending.
348 case Graphics::BlendFactor::SRC1_COLOR:
349 glFactor = GL_SRC_COLOR;
351 case Graphics::BlendFactor::ONE_MINUS_SRC1_COLOR:
352 glFactor = GL_ONE_MINUS_SRC_COLOR;
354 case Graphics::BlendFactor::SRC1_ALPHA:
355 glFactor = GL_SRC_ALPHA;
357 case Graphics::BlendFactor::ONE_MINUS_SRC1_ALPHA:
358 glFactor = GL_ONE_MINUS_SRC_ALPHA;
364 GLenum GetBlendOp(Graphics::BlendOp blendOp)
366 GLenum op = GL_FUNC_ADD;
369 case Graphics::BlendOp::ADD:
372 case Graphics::BlendOp::SUBTRACT:
373 op = GL_FUNC_SUBTRACT;
375 case Graphics::BlendOp::REVERSE_SUBTRACT:
376 op = GL_FUNC_REVERSE_SUBTRACT;
378 case Graphics::BlendOp::MIN:
381 case Graphics::BlendOp::MAX:
384 case Graphics::BlendOp::MULTIPLY:
389 case Graphics::BlendOp::SCREEN:
394 case Graphics::BlendOp::OVERLAY:
399 case Graphics::BlendOp::DARKEN:
404 case Graphics::BlendOp::LIGHTEN:
409 case Graphics::BlendOp::COLOR_DODGE:
414 case Graphics::BlendOp::COLOR_BURN:
419 case Graphics::BlendOp::HARD_LIGHT:
424 case Graphics::BlendOp::SOFT_LIGHT:
429 case Graphics::BlendOp::DIFFERENCE:
434 case Graphics::BlendOp::EXCLUSION:
439 case Graphics::BlendOp::HUE:
444 case Graphics::BlendOp::SATURATION:
446 op = GL_HSL_SATURATION;
449 case Graphics::BlendOp::COLOR:
454 case Graphics::BlendOp::LUMINOSITY:
456 op = GL_HSL_LUMINOSITY;
465 constexpr explicit GLCompareOp(Graphics::CompareOp compareOp)
469 case Graphics::CompareOp::NEVER:
472 case Graphics::CompareOp::LESS:
475 case Graphics::CompareOp::EQUAL:
478 case Graphics::CompareOp::LESS_OR_EQUAL:
481 case Graphics::CompareOp::GREATER:
484 case Graphics::CompareOp::NOT_EQUAL:
487 case Graphics::CompareOp::GREATER_OR_EQUAL:
490 case Graphics::CompareOp::ALWAYS:
500 constexpr explicit GLStencilOp(Graphics::StencilOp stencilOp)
504 case Graphics::StencilOp::KEEP:
507 case Graphics::StencilOp::ZERO:
510 case Graphics::StencilOp::REPLACE:
513 case Graphics::StencilOp::INCREMENT_AND_CLAMP:
516 case Graphics::StencilOp::DECREMENT_AND_CLAMP:
519 case Graphics::StencilOp::INVERT:
522 case Graphics::StencilOp::INCREMENT_AND_WRAP:
525 case Graphics::StencilOp::DECREMENT_AND_WRAP:
533 class TestGraphicsMemory : public Graphics::Memory
536 TestGraphicsMemory(TraceCallStack& callStack, TestGraphicsBuffer& buffer, uint32_t mappedOffset, uint32_t mappedSize)
537 : mCallStack(callStack),
539 mMappedOffset(mappedOffset),
540 mMappedSize(mappedSize),
546 void* LockRegion(uint32_t offset, uint32_t size) override
548 std::ostringstream o;
549 o << offset << ", " << size;
550 mCallStack.PushCall("Memory::LockRegion", o.str());
552 if(offset > mMappedOffset + mMappedSize ||
553 size + offset > mMappedOffset + mMappedSize)
555 fprintf(stderr, "TestGraphics.Memory::LockRegion() Out of bounds");
556 mBuffer.memory.resize(mMappedOffset + offset + size); // Grow to prevent memcpy from crashing
558 mLockedOffset = offset;
560 return &mBuffer.memory[mMappedOffset + offset];
563 void Unlock(bool flush) override
565 mCallStack.PushCall("Memory::Unlock", (flush ? "Flush" : "NoFlush"));
572 void Flush() override
574 mCallStack.PushCall("Memory::Flush", "");
576 mBuffer.Upload(mMappedOffset + mLockedOffset, mLockedSize);
580 TraceCallStack& mCallStack;
581 TestGraphicsBuffer& mBuffer;
582 uint32_t mMappedOffset;
583 uint32_t mMappedSize;
584 uint32_t mLockedOffset;
585 uint32_t mLockedSize;
588 TestGraphicsController::TestGraphicsController()
589 : mCallStack(false, "TestGraphicsController."),
590 mCommandBufferCallStack(false, "TestCommandBuffer."),
591 mFrameBufferCallStack(false, "TestFrameBuffer.")
593 mCallStack.Enable(true);
594 mCommandBufferCallStack.Enable(true);
595 auto& trace = mGl.GetTextureTrace();
597 trace.EnableLogging(false);
600 void TestGraphicsController::SubmitCommandBuffers(const Graphics::SubmitInfo& submitInfo)
602 TraceCallStack::NamedParams namedParams;
603 namedParams["submitInfo"] << "cmdBuffer[" << submitInfo.cmdBuffer.size()
604 << "], flags:" << std::hex << submitInfo.flags;
606 mCallStack.PushCall("SubmitCommandBuffers", "", namedParams);
608 mSubmitStack.emplace_back(submitInfo);
610 for(auto& graphicsCommandBuffer : submitInfo.cmdBuffer)
612 auto commandBuffer = Uncast<TestGraphicsCommandBuffer>(graphicsCommandBuffer);
613 ProcessCommandBuffer(*commandBuffer);
617 void TestGraphicsController::ProcessCommandBuffer(TestGraphicsCommandBuffer& commandBuffer)
619 bool scissorEnabled = false;
620 TestGraphicsFramebuffer* currentFramebuffer{nullptr};
621 TestGraphicsPipeline* currentPipeline{nullptr};
623 for(auto& cmd : commandBuffer.GetCommands())
628 case CommandType::FLUSH:
630 // Nothing to do here
633 case CommandType::BIND_TEXTURES:
635 for(auto& binding : cmd.data.bindTextures.textureBindings)
639 auto texture = Uncast<TestGraphicsTexture>(binding.texture);
640 texture->Bind(binding.binding);
644 auto sampler = Uncast<TestGraphicsSampler>(binding.sampler);
647 sampler->Apply(texture->GetTarget());
651 texture->Prepare(); // Ensure native texture is ready
656 case CommandType::BIND_VERTEX_BUFFERS:
658 for(auto& binding : cmd.data.bindVertexBuffers.vertexBufferBindings)
660 auto graphicsBuffer = binding.buffer;
661 auto vertexBuffer = Uncast<TestGraphicsBuffer>(graphicsBuffer);
662 vertexBuffer->Bind();
666 case CommandType::BIND_INDEX_BUFFER:
668 auto& indexBufferBinding = cmd.data.bindIndexBuffer;
669 if(indexBufferBinding.buffer)
671 auto buffer = Uncast<TestGraphicsBuffer>(indexBufferBinding.buffer);
676 case CommandType::BIND_UNIFORM_BUFFER:
680 auto& bindings = cmd.data.bindUniformBuffers;
681 auto buffer = bindings.standaloneUniformsBufferBinding;
683 // based on reflection, issue gl calls
684 buffer.buffer->BindAsUniformBuffer(static_cast<const TestGraphicsProgram*>(currentPipeline->programState.program), bindings.standaloneUniformsBufferBinding);
688 case CommandType::BIND_SAMPLERS:
692 case CommandType::BIND_PIPELINE:
694 currentPipeline = Uncast<TestGraphicsPipeline>(cmd.data.bindPipeline.pipeline);
695 BindPipeline(currentPipeline);
698 case CommandType::DRAW_NATIVE:
700 auto info = &cmd.data.draw.drawNative.drawNativeInfo;
702 if(info->glesNativeInfo.eglSharedContextStoragePointer)
704 auto* anyContext = reinterpret_cast<std::any*>(info->glesNativeInfo.eglSharedContextStoragePointer);
705 *anyContext = reinterpret_cast<void*>(0x12345678u);
708 CallbackBase::ExecuteReturn<bool>(*info->callback, info->userData);
711 case CommandType::DRAW:
715 if(cmd.data.draw.draw.instanceCount == 0)
717 mGl.DrawArrays(GetTopology(currentPipeline->inputAssemblyState.topology),
718 cmd.data.draw.draw.firstVertex,
719 cmd.data.draw.draw.vertexCount);
723 mGl.DrawArraysInstanced(GetTopology(currentPipeline->inputAssemblyState.topology),
724 cmd.data.draw.draw.firstVertex,
725 cmd.data.draw.draw.vertexCount,
726 cmd.data.draw.draw.instanceCount);
731 case CommandType::DRAW_INDEXED:
735 if(cmd.data.draw.draw.instanceCount == 0)
737 mGl.DrawElements(GetTopology(currentPipeline->inputAssemblyState.topology),
738 static_cast<GLsizei>(cmd.data.draw.drawIndexed.indexCount),
740 reinterpret_cast<void*>(cmd.data.draw.drawIndexed.firstIndex));
744 mGl.DrawElementsInstanced(GetTopology(currentPipeline->inputAssemblyState.topology),
745 static_cast<GLsizei>(cmd.data.draw.drawIndexed.indexCount),
747 reinterpret_cast<void*>(cmd.data.draw.drawIndexed.firstIndex),
748 cmd.data.draw.drawIndexed.instanceCount);
753 case CommandType::DRAW_INDEXED_INDIRECT:
757 mGl.DrawElements(GetTopology(currentPipeline->inputAssemblyState.topology),
758 static_cast<GLsizei>(cmd.data.draw.drawIndexed.indexCount),
760 reinterpret_cast<void*>(cmd.data.draw.drawIndexed.firstIndex));
764 case CommandType::SET_SCISSOR:
768 auto& rect = cmd.data.scissor.region;
769 mGl.Scissor(rect.x, rect.y, rect.width, rect.height);
773 case CommandType::SET_SCISSOR_TEST:
775 if(cmd.data.scissorTest.enable)
777 mGl.Enable(GL_SCISSOR_TEST);
778 scissorEnabled = true;
782 mGl.Disable(GL_SCISSOR_TEST);
783 scissorEnabled = false;
787 case CommandType::SET_VIEWPORT_TEST:
791 case CommandType::SET_VIEWPORT: // @todo Consider correcting for orientation here?
793 auto& rect = cmd.data.viewport.region;
794 mGl.Viewport(rect.x, rect.y, rect.width, rect.height);
798 case CommandType::SET_COLOR_MASK:
800 // Set all channels to the same mask
801 const bool mask = cmd.data.colorMask.enabled;
802 mGl.ColorMask(mask, mask, mask, mask);
805 case CommandType::CLEAR_STENCIL_BUFFER:
807 mGl.Clear(GL_STENCIL_BUFFER_BIT);
810 case CommandType::CLEAR_DEPTH_BUFFER:
812 mGl.Clear(GL_DEPTH_BUFFER_BIT);
816 case CommandType::SET_STENCIL_TEST_ENABLE:
818 if(cmd.data.stencilTest.enabled)
820 mGl.Enable(GL_STENCIL_TEST);
824 mGl.Disable(GL_STENCIL_TEST);
829 case CommandType::SET_STENCIL_FUNC:
831 mGl.StencilFunc(GLCompareOp(cmd.data.stencilFunc.compareOp).op,
832 cmd.data.stencilFunc.reference,
833 cmd.data.stencilFunc.compareMask);
837 case CommandType::SET_STENCIL_WRITE_MASK:
839 mGl.StencilMask(cmd.data.stencilWriteMask.mask);
842 case CommandType::SET_STENCIL_OP:
844 mGl.StencilOp(GLStencilOp(cmd.data.stencilOp.failOp).op,
845 GLStencilOp(cmd.data.stencilOp.depthFailOp).op,
846 GLStencilOp(cmd.data.stencilOp.passOp).op);
850 case CommandType::SET_DEPTH_COMPARE_OP:
852 mGl.DepthFunc(GLCompareOp(cmd.data.depth.compareOp).op);
855 case CommandType::SET_DEPTH_TEST_ENABLE:
857 if(cmd.data.depth.testEnabled)
859 mGl.Enable(GL_DEPTH_TEST);
863 mGl.Disable(GL_DEPTH_TEST);
867 case CommandType::SET_DEPTH_WRITE_ENABLE:
869 mGl.DepthMask(cmd.data.depth.writeEnabled);
873 case CommandType::EXECUTE_COMMAND_BUFFERS:
875 // Process secondary command buffers
876 for(auto& buf : cmd.data.executeCommandBuffers.buffers)
878 ProcessCommandBuffer(*Uncast<TestGraphicsCommandBuffer>(buf));
882 case CommandType::BEGIN_RENDER_PASS:
884 auto renderTarget = Uncast<TestGraphicsRenderTarget>(cmd.data.beginRenderPass.renderTarget);
888 auto fb = renderTarget->mCreateInfo.framebuffer;
891 if(currentFramebuffer != fb)
893 currentFramebuffer = Uncast<TestGraphicsFramebuffer>(fb);
894 currentFramebuffer->Bind();
899 mGl.BindFramebuffer(GL_FRAMEBUFFER, 0);
904 mGl.BindFramebuffer(GL_FRAMEBUFFER, 0);
907 auto& clearValues = cmd.data.beginRenderPass.clearValues;
908 if(clearValues.size() > 0)
910 const auto renderPass = static_cast<TestGraphicsRenderPass*>(cmd.data.beginRenderPass.renderPass);
913 const auto& color0 = renderPass->attachments[0];
915 if(color0.loadOp == Graphics::AttachmentLoadOp::CLEAR)
917 mask |= GL_COLOR_BUFFER_BIT;
919 // Set clear color (todo: cache it!)
920 // Something goes wrong here if Alpha mask is GL_TRUE
921 mGl.ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
922 mGl.ClearColor(clearValues[0].color.r,
923 clearValues[0].color.g,
924 clearValues[0].color.b,
925 clearValues[0].color.a);
928 // check for depth stencil
929 if(renderPass->attachments.size() > 1)
931 const auto& depthStencil = renderPass->attachments.back();
932 if(depthStencil.loadOp == Graphics::AttachmentLoadOp::CLEAR)
935 uint32_t depthClearColor = 0u;
936 if(clearValues.size() == renderPass->attachments.size())
938 depthClearColor = clearValues.back().depthStencil.depth;
940 mGl.ClearDepthf(depthClearColor);
941 mask |= GL_DEPTH_BUFFER_BIT;
943 if(depthStencil.stencilLoadOp == Graphics::AttachmentLoadOp::CLEAR)
945 uint32_t stencilClearColor = 0u;
946 if(clearValues.size() == renderPass->attachments.size())
948 stencilClearColor = clearValues.back().depthStencil.stencil;
950 mGl.ClearStencil(stencilClearColor);
951 mGl.StencilMask(0xFF); // Clear all the bitplanes (assume 8)
952 mask |= GL_STENCIL_BUFFER_BIT;
958 // Test scissor area and RT size
959 const auto& area = cmd.data.beginRenderPass.renderArea;
963 area.width == renderTarget->mCreateInfo.extent.width &&
964 area.height == renderTarget->mCreateInfo.extent.height)
966 mGl.Disable(GL_SCISSOR_TEST);
971 mGl.Enable(GL_SCISSOR_TEST);
972 mGl.Scissor(cmd.data.beginRenderPass.renderArea.x, cmd.data.beginRenderPass.renderArea.y, cmd.data.beginRenderPass.renderArea.width, cmd.data.beginRenderPass.renderArea.height);
974 mGl.Disable(GL_SCISSOR_TEST);
980 DALI_ASSERT_DEBUG(0 && "BeginRenderPass has no render pass");
985 case CommandType::END_RENDER_PASS:
987 if(cmd.data.endRenderPass.syncObject != nullptr)
989 auto syncObject = Uncast<TestGraphicsSyncObject>(cmd.data.endRenderPass.syncObject);
990 syncObject->InitializeResource(); // create the sync object.
998 void TestGraphicsController::BindPipeline(TestGraphicsPipeline* pipeline)
1000 auto& vi = pipeline->vertexInputState;
1001 for(auto& attribute : vi.attributes)
1003 mGl.EnableVertexAttribArray(attribute.location);
1004 uint32_t attributeOffset = attribute.offset;
1005 GLsizei stride = vi.bufferBindings[attribute.binding].stride;
1007 auto rate = vi.bufferBindings[attribute.binding].inputRate;
1009 mGl.VertexAttribPointer(attribute.location,
1010 GetNumComponents(attribute.format),
1011 GetGlType(attribute.format),
1012 GL_FALSE, // Not normalized
1014 reinterpret_cast<void*>(attributeOffset));
1015 if(rate == Graphics::VertexInputRate::PER_VERTEX)
1017 mGl.VertexAttribDivisor(attribute.location, 0);
1019 else if(rate == Graphics::VertexInputRate::PER_INSTANCE)
1021 mGl.VertexAttribDivisor(attribute.location, 1);
1026 auto& rasterizationState = pipeline->rasterizationState;
1027 if(rasterizationState.cullMode == Graphics::CullMode::NONE)
1029 mGl.Disable(GL_CULL_FACE);
1033 mGl.Enable(GL_CULL_FACE);
1034 mGl.CullFace(GetCullFace(rasterizationState.cullMode));
1037 mGl.FrontFace(GetFrontFace(rasterizationState.frontFace));
1040 auto& colorBlendState = pipeline->colorBlendState;
1041 if(colorBlendState.blendEnable)
1043 mGl.Enable(GL_BLEND);
1045 mGl.BlendFuncSeparate(GetBlendFactor(colorBlendState.srcColorBlendFactor),
1046 GetBlendFactor(colorBlendState.dstColorBlendFactor),
1047 GetBlendFactor(colorBlendState.srcAlphaBlendFactor),
1048 GetBlendFactor(colorBlendState.dstAlphaBlendFactor));
1049 if(colorBlendState.colorBlendOp != colorBlendState.alphaBlendOp)
1051 mGl.BlendEquationSeparate(GetBlendOp(colorBlendState.colorBlendOp), GetBlendOp(colorBlendState.alphaBlendOp));
1055 mGl.BlendEquation(GetBlendOp(colorBlendState.colorBlendOp));
1057 mGl.BlendColor(colorBlendState.blendConstants[0],
1058 colorBlendState.blendConstants[1],
1059 colorBlendState.blendConstants[2],
1060 colorBlendState.blendConstants[3]);
1064 mGl.Disable(GL_BLEND);
1067 auto* program = static_cast<const TestGraphicsProgram*>(pipeline->programState.program);
1068 mGl.UseProgram(program->mImpl->mId);
1072 * @brief Presents render target
1073 * @param renderTarget render target to present
1075 void TestGraphicsController::PresentRenderTarget(Graphics::RenderTarget* renderTarget)
1077 auto* rt = static_cast<const TestGraphicsRenderTarget*>(renderTarget);
1078 TraceCallStack::NamedParams namedParams;
1079 namedParams["renderTarget"] << std::hex << renderTarget;
1080 namedParams["surface"] << std::hex << rt->mCreateInfo.surface;
1081 mCallStack.PushCall("PresentRenderTarget", namedParams.str(), namedParams);
1085 * @brief Waits until the GPU is idle
1087 void TestGraphicsController::WaitIdle()
1089 mCallStack.PushCall("WaitIdle", "");
1093 * @brief Lifecycle pause event
1095 void TestGraphicsController::Pause()
1097 mCallStack.PushCall("Pause", "");
1101 * @brief Lifecycle resume event
1103 void TestGraphicsController::Resume()
1105 mCallStack.PushCall("Resume", "");
1108 void TestGraphicsController::Shutdown()
1110 mCallStack.PushCall("Shutdown", "");
1113 void TestGraphicsController::Destroy()
1115 mCallStack.PushCall("Destroy", "");
1118 void TestGraphicsController::UpdateTextures(const std::vector<Graphics::TextureUpdateInfo>& updateInfoList,
1119 const std::vector<Graphics::TextureUpdateSourceInfo>& sourceList)
1121 TraceCallStack::NamedParams namedParams;
1122 namedParams["updateInfoList"] << "[" << updateInfoList.size() << "]:";
1123 namedParams["sourceList"] << "[" << sourceList.size() << "]:";
1125 mCallStack.PushCall("UpdateTextures", "", namedParams);
1127 // Call either TexImage2D or TexSubImage2D
1128 for(unsigned int i = 0; i < updateInfoList.size(); ++i)
1130 auto& updateInfo = updateInfoList[i];
1131 auto& source = sourceList[i];
1133 auto texture = static_cast<TestGraphicsTexture*>(updateInfo.dstTexture);
1134 texture->Bind(0); // Use first texture unit during resource update
1135 texture->Update(updateInfo, source);
1139 void TestGraphicsController::GenerateTextureMipmaps(const Graphics::Texture& texture)
1141 mCallStack.PushCall("GenerateTextureMipmaps", "");
1143 auto gfxTexture = Uncast<TestGraphicsTexture>(&texture);
1144 mGl.BindTexture(gfxTexture->GetTarget(), 0);
1145 mGl.GenerateMipmap(gfxTexture->GetTarget());
1148 bool TestGraphicsController::EnableDepthStencilBuffer(bool enableDepth, bool enableStencil)
1150 TraceCallStack::NamedParams namedParams;
1151 namedParams["enableDepth"] << (enableDepth ? "T" : "F");
1152 namedParams["enableStencil"] << (enableStencil ? "T" : "F");
1153 mCallStack.PushCall("EnableDepthStencilBuffer", "", namedParams);
1157 void TestGraphicsController::RunGarbageCollector(size_t numberOfDiscardedRenderers)
1159 TraceCallStack::NamedParams namedParams;
1160 namedParams["numberOfDiscardedRenderers"] << numberOfDiscardedRenderers;
1161 mCallStack.PushCall("RunGarbageCollector", "", namedParams);
1164 void TestGraphicsController::DiscardUnusedResources()
1166 mCallStack.PushCall("DiscardUnusedResources", "");
1169 bool TestGraphicsController::IsDiscardQueueEmpty()
1171 mCallStack.PushCall("IsDiscardQueueEmpty", "");
1172 return isDiscardQueueEmptyResult;
1176 * @brief Test if the graphics subsystem has resumed & should force a draw
1178 * @return true if the graphics subsystem requires a re-draw
1180 bool TestGraphicsController::IsDrawOnResumeRequired()
1182 mCallStack.PushCall("IsDrawOnResumeRequired", "");
1183 return isDrawOnResumeRequiredResult;
1186 Graphics::UniquePtr<Graphics::Buffer> TestGraphicsController::CreateBuffer(const Graphics::BufferCreateInfo& createInfo, Graphics::UniquePtr<Graphics::Buffer>&& oldBuffer)
1188 std::ostringstream oss;
1189 oss << "bufferCreateInfo:" << createInfo;
1190 mCallStack.PushCall("CreateBuffer", oss.str());
1191 return Graphics::MakeUnique<TestGraphicsBuffer>(mCallStack, mGl, createInfo.size, createInfo.usage);
1194 Graphics::UniquePtr<Graphics::CommandBuffer> TestGraphicsController::CreateCommandBuffer(const Graphics::CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<Graphics::CommandBuffer>&& oldCommandBuffer)
1196 std::ostringstream oss;
1197 oss << "commandBufferCreateInfo:" << commandBufferCreateInfo;
1198 mCallStack.PushCall("CreateCommandBuffer", oss.str());
1199 return Graphics::MakeUnique<TestGraphicsCommandBuffer>(mCommandBufferCallStack, mGl);
1202 Graphics::UniquePtr<Graphics::RenderPass> TestGraphicsController::CreateRenderPass(const Graphics::RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<Graphics::RenderPass>&& oldRenderPass)
1204 mCallStack.PushCall("CreateRenderPass", "");
1205 return Graphics::MakeUnique<TestGraphicsRenderPass>(mGl, renderPassCreateInfo);
1208 Graphics::UniquePtr<Graphics::Texture> TestGraphicsController::CreateTexture(const Graphics::TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Graphics::Texture>&& oldTexture)
1210 TraceCallStack::NamedParams namedParams;
1211 namedParams["textureCreateInfo"] << textureCreateInfo;
1212 mCallStack.PushCall("CreateTexture", namedParams.str(), namedParams);
1214 return Graphics::MakeUnique<TestGraphicsTexture>(mGl, textureCreateInfo);
1217 Graphics::UniquePtr<Graphics::Framebuffer> TestGraphicsController::CreateFramebuffer(
1218 const Graphics::FramebufferCreateInfo& createInfo,
1219 Graphics::UniquePtr<Graphics::Framebuffer>&& oldFramebuffer)
1221 TraceCallStack::NamedParams namedParams;
1222 namedParams["framebufferCreateInfo"] << createInfo;
1223 mCallStack.PushCall("Controller::CreateFramebuffer", namedParams.str(), namedParams);
1225 return Graphics::MakeUnique<TestGraphicsFramebuffer>(mFrameBufferCallStack, mGl, createInfo);
1228 Graphics::UniquePtr<Graphics::Pipeline> TestGraphicsController::CreatePipeline(const Graphics::PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline)
1230 mCallStack.PushCall("CreatePipeline", "");
1231 return std::make_unique<TestGraphicsPipeline>(mGl, pipelineCreateInfo);
1234 Graphics::UniquePtr<Graphics::Program> TestGraphicsController::CreateProgram(const Graphics::ProgramCreateInfo& programCreateInfo, Graphics::UniquePtr<Graphics::Program>&& oldProgram)
1236 mCallStack.PushCall("CreateProgram", "");
1238 for(auto cacheEntry : mProgramCache)
1241 for(auto& shader : *(programCreateInfo.shaderState))
1243 auto graphicsShader = Uncast<TestGraphicsShader>(shader.shader);
1244 std::vector<uint8_t> source;
1245 source.resize(graphicsShader->mCreateInfo.sourceSize);
1246 memcpy(&source[0], graphicsShader->mCreateInfo.sourceData, graphicsShader->mCreateInfo.sourceSize);
1248 if(!std::equal(source.begin(), source.end(), cacheEntry.shaders[shader.pipelineStage].begin(), cacheEntry.shaders[shader.pipelineStage].end()))
1256 return Graphics::MakeUnique<TestGraphicsProgram>(cacheEntry.programImpl);
1260 mProgramCache.emplace_back();
1261 mProgramCache.back().programImpl = new TestGraphicsProgramImpl(mGl, programCreateInfo, mVertexFormats, mCustomUniforms);
1262 for(auto& shader : *(programCreateInfo.shaderState))
1264 auto graphicsShader = Uncast<TestGraphicsShader>(shader.shader);
1265 mProgramCache.back().shaders[shader.pipelineStage].resize(graphicsShader->mCreateInfo.sourceSize);
1266 memcpy(&mProgramCache.back().shaders[shader.pipelineStage][0], graphicsShader->mCreateInfo.sourceData, graphicsShader->mCreateInfo.sourceSize);
1268 return Graphics::MakeUnique<TestGraphicsProgram>(mProgramCache.back().programImpl);
1271 Graphics::UniquePtr<Graphics::Shader> TestGraphicsController::CreateShader(const Graphics::ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Graphics::Shader>&& oldShader)
1273 mCallStack.PushCall("CreateShader", "");
1274 return Graphics::MakeUnique<TestGraphicsShader>(mGl, shaderCreateInfo);
1277 Graphics::UniquePtr<Graphics::Sampler> TestGraphicsController::CreateSampler(const Graphics::SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Graphics::Sampler>&& oldSampler)
1279 TraceCallStack::NamedParams namedParams;
1280 namedParams["samplerCreateInfo"] << samplerCreateInfo;
1281 mCallStack.PushCall("CreateSampler", namedParams.str(), namedParams);
1283 return Graphics::MakeUnique<TestGraphicsSampler>(mGl, samplerCreateInfo);
1286 Graphics::UniquePtr<Graphics::RenderTarget> TestGraphicsController::CreateRenderTarget(const Graphics::RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<Graphics::RenderTarget>&& oldRenderTarget)
1288 TraceCallStack::NamedParams namedParams;
1289 namedParams["surface"] << std::hex << renderTargetCreateInfo.surface;
1290 mCallStack.PushCall("CreateRenderTarget", namedParams.str(), namedParams);
1292 return Graphics::MakeUnique<TestGraphicsRenderTarget>(mGl, renderTargetCreateInfo);
1295 Graphics::UniquePtr<Graphics::SyncObject> TestGraphicsController::CreateSyncObject(
1296 const Graphics::SyncObjectCreateInfo& syncObjectCreateInfo,
1297 Graphics::UniquePtr<Graphics::SyncObject>&& oldSyncObject)
1299 mCallStack.PushCall("CreateSyncObject", "");
1300 return Graphics::MakeUnique<TestGraphicsSyncObject>(mGraphicsSyncImpl, syncObjectCreateInfo);
1303 Graphics::UniquePtr<Graphics::Memory> TestGraphicsController::MapBufferRange(const Graphics::MapBufferInfo& mapInfo)
1305 mCallStack.PushCall("MapBufferRange", "");
1307 auto buffer = static_cast<TestGraphicsBuffer*>(mapInfo.buffer);
1308 buffer->memory.resize(mapInfo.offset + mapInfo.size); // For initial testing, allow writes past capacity
1310 return std::make_unique<TestGraphicsMemory>(mCallStack, *buffer, mapInfo.offset, mapInfo.size);
1313 Graphics::UniquePtr<Graphics::Memory> TestGraphicsController::MapTextureRange(const Graphics::MapTextureInfo& mapInfo)
1315 mCallStack.PushCall("MapTextureRange", "");
1319 void TestGraphicsController::UnmapMemory(Graphics::UniquePtr<Graphics::Memory> memory)
1321 mCallStack.PushCall("UnmapMemory", "");
1324 Graphics::MemoryRequirements TestGraphicsController::GetTextureMemoryRequirements(Graphics::Texture& texture) const
1326 mCallStack.PushCall("GetTextureMemoryRequirements", "");
1327 return Graphics::MemoryRequirements{};
1330 Graphics::MemoryRequirements TestGraphicsController::GetBufferMemoryRequirements(Graphics::Buffer& buffer) const
1332 mCallStack.PushCall("GetBufferMemoryRequirements", "");
1333 return Graphics::MemoryRequirements{};
1336 Graphics::TextureProperties TestGraphicsController::GetTextureProperties(const Graphics::Texture& texture)
1338 static Graphics::TextureProperties textureProperties{};
1339 mCallStack.PushCall("GetTextureProperties", "");
1341 return textureProperties;
1344 const Graphics::Reflection& TestGraphicsController::GetProgramReflection(const Graphics::Program& program)
1346 mCallStack.PushCall("GetProgramReflection", "");
1348 return static_cast<const TestGraphicsProgram*>(&program)->GetReflection();
1351 bool TestGraphicsController::PipelineEquals(const Graphics::Pipeline& pipeline0, const Graphics::Pipeline& pipeline1) const
1353 mCallStack.PushCall("PipelineEquals", "");
1357 bool TestGraphicsController::GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData)
1359 mCallStack.PushCall("GetProgramParameter", "");
1360 auto graphicsProgram = Uncast<TestGraphicsProgram>(&program);
1361 return graphicsProgram->GetParameter(parameterId, outData);