1 #ifndef DALI_TEST_GRAPHICS_COMMAND_BUFFER_H
2 #define DALI_TEST_GRAPHICS_COMMAND_BUFFER_H
5 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #include <dali/graphics-api/graphics-command-buffer-create-info.h>
21 #include <dali/graphics-api/graphics-command-buffer.h>
22 #include <dali/graphics-api/graphics-pipeline.h>
23 #include <dali/graphics-api/graphics-types.h>
26 #include "test-gl-abstraction.h"
27 #include "test-graphics-pipeline.h"
28 #include "test-graphics-buffer.h"
29 #include "test-trace-call-stack.h"
33 class TestGraphicsTexture;
34 class TestGraphicsBuffer;
35 class TestGraphicsSampler;
36 class TestGraphicsPipeline;
38 enum class CommandType
41 BIND_TEXTURES = 1 << 1,
42 BIND_SAMPLERS = 1 << 2,
43 BIND_VERTEX_BUFFERS = 1 << 3,
44 BIND_INDEX_BUFFER = 1 << 4,
45 BIND_UNIFORM_BUFFER = 1 << 5,
46 BIND_PIPELINE = 1 << 6,
48 DRAW_INDEXED = 1 << 8,
49 DRAW_INDEXED_INDIRECT = 1 << 9
52 using CommandTypeMask = uint32_t;
54 inline CommandTypeMask operator|(T flags, CommandType bit)
56 return static_cast<CommandTypeMask>(flags) | static_cast<CommandTypeMask>(bit);
60 * @brief Descriptor of single buffer binding within
63 struct VertexBufferBindingDescriptor
65 const TestGraphicsBuffer* buffer{nullptr};
70 * @brief Descriptor of ix buffer binding within
73 struct IndexBufferBindingDescriptor
75 const TestGraphicsBuffer* buffer{nullptr};
77 Graphics::Format format{};
81 * @brief Descriptor of uniform buffer binding within
84 struct UniformBufferBindingDescriptor
86 const TestGraphicsBuffer* buffer{nullptr};
89 bool emulated; ///<true if UBO is emulated for old gfx API
93 * @brief The descriptor of draw call
95 struct DrawCallDescriptor
98 * @brief Enum specifying type of the draw call
104 DRAW_INDEXED_INDIRECT
107 Type type{}; ///< Type of the draw call
110 * Union contains data for all types of draw calls.
115 * @brief Vertex array draw
119 uint32_t vertexCount;
120 uint32_t instanceCount;
121 uint32_t firstVertex;
122 uint32_t firstInstance;
126 * @brief Indexed draw
131 uint32_t instanceCount;
133 int32_t vertexOffset;
134 uint32_t firstInstance;
138 * @brief Indexed draw indirect
142 const TestGraphicsBuffer* buffer;
146 } drawIndexedIndirect;
151 * Command structure allocates memory to store a single command
164 * @brief Copy constructor
165 * @param[in] rhs Command
167 Command(const Command& rhs)
171 case CommandType::BIND_VERTEX_BUFFERS:
173 bindVertexBuffers = rhs.bindVertexBuffers;
176 case CommandType::BIND_INDEX_BUFFER:
178 bindIndexBuffer = rhs.bindIndexBuffer;
181 case CommandType::BIND_SAMPLERS:
183 bindSamplers = rhs.bindSamplers;
186 case CommandType::BIND_TEXTURES:
188 bindTextures = rhs.bindTextures;
191 case CommandType::BIND_PIPELINE:
193 bindPipeline = rhs.bindPipeline;
196 case CommandType::BIND_UNIFORM_BUFFER:
198 bindUniformBuffers = rhs.bindUniformBuffers;
201 case CommandType::DRAW:
203 draw.type = rhs.draw.type;
204 draw.draw = rhs.draw.draw;
207 case CommandType::DRAW_INDEXED:
209 draw.type = rhs.draw.type;
210 draw.drawIndexed = rhs.draw.drawIndexed;
213 case CommandType::DRAW_INDEXED_INDIRECT:
215 draw.type = rhs.draw.type;
216 draw.drawIndexedIndirect = rhs.draw.drawIndexedIndirect;
219 case CommandType::FLUSH:
229 * @brief Copy constructor
230 * @param[in] rhs Command
232 Command(Command&& rhs) noexcept
236 case CommandType::BIND_VERTEX_BUFFERS:
238 bindVertexBuffers = std::move(rhs.bindVertexBuffers);
241 case CommandType::BIND_INDEX_BUFFER:
243 bindIndexBuffer = rhs.bindIndexBuffer;
246 case CommandType::BIND_UNIFORM_BUFFER:
248 bindUniformBuffers = std::move(rhs.bindUniformBuffers);
251 case CommandType::BIND_SAMPLERS:
253 bindSamplers = std::move(rhs.bindSamplers);
256 case CommandType::BIND_TEXTURES:
258 bindTextures = std::move(rhs.bindTextures);
261 case CommandType::BIND_PIPELINE:
263 bindPipeline = rhs.bindPipeline;
266 case CommandType::DRAW:
268 draw.type = rhs.draw.type;
269 draw.draw = rhs.draw.draw;
272 case CommandType::DRAW_INDEXED:
274 draw.type = rhs.draw.type;
275 draw.drawIndexed = rhs.draw.drawIndexed;
278 case CommandType::DRAW_INDEXED_INDIRECT:
280 draw.type = rhs.draw.type;
281 draw.drawIndexedIndirect = rhs.draw.drawIndexedIndirect;
284 case CommandType::FLUSH:
293 CommandType type{CommandType::FLUSH}; ///< Type of command
299 std::vector<Graphics::TextureBinding> textureBindings;
302 // BindSampler command
305 std::vector<Graphics::SamplerBinding> samplerBindings;
310 using Binding = VertexBufferBindingDescriptor;
311 std::vector<Binding> vertexBufferBindings;
314 struct : public IndexBufferBindingDescriptor
320 std::vector<UniformBufferBindingDescriptor> uniformBufferBindings{};
321 UniformBufferBindingDescriptor standaloneUniformsBufferBinding{};
322 } bindUniformBuffers;
326 const TestGraphicsPipeline* pipeline{nullptr};
329 struct : public DrawCallDescriptor
336 class TestGraphicsCommandBuffer : public Graphics::CommandBuffer
339 TestGraphicsCommandBuffer(TraceCallStack& callstack, TestGlAbstraction& glAbstraction);
340 ~TestGraphicsCommandBuffer()
345 void BindVertexBuffers(uint32_t firstBinding,
346 std::vector<const Graphics::Buffer*> buffers,
347 std::vector<uint32_t> offsets) override
349 mCommands.emplace_back();
350 mCommands.back().type = CommandType::BIND_VERTEX_BUFFERS;
351 auto& bindings = mCommands.back().bindVertexBuffers.vertexBufferBindings;
352 if(bindings.size() < firstBinding + buffers.size())
354 bindings.resize(firstBinding + buffers.size());
355 auto index = firstBinding;
356 for(auto& buf : buffers)
358 bindings[index].buffer = static_cast<const TestGraphicsBuffer*>(buf);
359 bindings[index].offset = offsets[index - firstBinding];
363 mCallStack.PushCall("BindVertexBuffers", "");
366 void BindUniformBuffers(const std::vector<Graphics::UniformBufferBinding>& bindings) override
368 mCommands.emplace_back();
369 auto& cmd = mCommands.back();
370 cmd.type = CommandType::BIND_UNIFORM_BUFFER;
371 auto& bindCmd = cmd.bindUniformBuffers;
372 for(const auto& binding : bindings)
376 auto testBuffer = static_cast<const TestGraphicsBuffer*>(binding.buffer);
377 if(testBuffer->IsCPUAllocated()) // standalone uniforms
379 bindCmd.standaloneUniformsBufferBinding.buffer = testBuffer;
380 bindCmd.standaloneUniformsBufferBinding.offset = binding.offset;
381 bindCmd.standaloneUniformsBufferBinding.binding = binding.binding;
382 bindCmd.standaloneUniformsBufferBinding.emulated = true;
384 else // Bind regular UBO
386 // resize binding slots
387 if(binding.binding >= bindCmd.uniformBufferBindings.size())
389 bindCmd.uniformBufferBindings.resize(binding.binding + 1);
391 auto& slot = bindCmd.uniformBufferBindings[binding.binding];
392 slot.buffer = testBuffer;
393 slot.offset = binding.offset;
394 slot.binding = binding.binding;
395 slot.emulated = false;
399 mCallStack.PushCall("BindUniformBuffers", "");
402 void BindPipeline(const Graphics::Pipeline& pipeline) override
404 mCommands.emplace_back();
405 mCommands.back().type = CommandType::BIND_PIPELINE;
406 mCommands.back().bindPipeline.pipeline = static_cast<const TestGraphicsPipeline*>(&pipeline);
407 mCallStack.PushCall("BindPipeline", "");
410 void BindTextures(std::vector<Graphics::TextureBinding>& textureBindings) override
412 mCommands.emplace_back();
413 mCommands.back().type = CommandType::BIND_TEXTURES;
414 mCommands.back().bindTextures.textureBindings = std::move(textureBindings);
415 mCallStack.PushCall("BindTextures", "");
418 void BindSamplers(std::vector<Graphics::SamplerBinding>& samplerBindings) override
420 mCommands.emplace_back();
421 mCommands.back().bindSamplers.samplerBindings = std::move(samplerBindings);
422 mCallStack.PushCall("BindSamplers", "");
425 void BindPushConstants(void* data,
427 uint32_t binding) override
429 mCallStack.PushCall("BindPushConstants", "");
432 void BindIndexBuffer(const Graphics::Buffer& buffer,
434 Graphics::Format format) override
436 mCommands.emplace_back();
437 mCommands.back().type = CommandType::BIND_INDEX_BUFFER;
438 mCommands.back().bindIndexBuffer.buffer = static_cast<const TestGraphicsBuffer*>(&buffer);
439 mCommands.back().bindIndexBuffer.offset = offset;
440 mCommands.back().bindIndexBuffer.format = format;
441 mCallStack.PushCall("BindIndexBuffer", "");
444 void BeginRenderPass(
445 Graphics::RenderPass& renderPass,
446 Graphics::RenderTarget& renderTarget,
447 Graphics::Extent2D renderArea,
448 std::vector<Graphics::ClearValue> clearValues) override
450 mCallStack.PushCall("BeginRenderPass", "");
454 * @brief Ends current render pass
456 * This command must be issued in order to finalize the render pass.
457 * It's up to the implementation whether anything has to be done but
458 * the Controller may use end RP marker in order to resolve resource
459 * dependencies (for example, to know when target texture is ready
460 * before passing it to another render pass).
462 void EndRenderPass() override
464 mCallStack.PushCall("EndRenderPass", "");
468 uint32_t vertexCount,
469 uint32_t instanceCount,
470 uint32_t firstVertex,
471 uint32_t firstInstance) override
473 mCommands.emplace_back();
474 mCommands.back().type = CommandType::DRAW;
475 auto& cmd = mCommands.back().draw;
476 cmd.type = DrawCallDescriptor::Type::DRAW;
477 cmd.draw.vertexCount = vertexCount;
478 cmd.draw.instanceCount = instanceCount;
479 cmd.draw.firstInstance = firstInstance;
480 cmd.draw.firstVertex = firstVertex;
481 mCallStack.PushCall("Draw", "");
486 uint32_t instanceCount,
488 int32_t vertexOffset,
489 uint32_t firstInstance) override
491 mCommands.emplace_back();
492 mCommands.back().type = CommandType::DRAW_INDEXED;
493 auto& cmd = mCommands.back().draw;
494 cmd.type = DrawCallDescriptor::Type::DRAW_INDEXED;
495 cmd.drawIndexed.firstIndex = firstIndex;
496 cmd.drawIndexed.firstInstance = firstInstance;
497 cmd.drawIndexed.indexCount = indexCount;
498 cmd.drawIndexed.vertexOffset = vertexOffset;
499 cmd.drawIndexed.instanceCount = instanceCount;
500 mCallStack.PushCall("DrawIndexed", "");
503 void DrawIndexedIndirect(
504 Graphics::Buffer& buffer,
507 uint32_t stride) override
509 mCommands.emplace_back();
510 mCommands.back().type = CommandType::DRAW_INDEXED_INDIRECT;
511 auto& cmd = mCommands.back().draw;
512 cmd.type = DrawCallDescriptor::Type::DRAW_INDEXED_INDIRECT;
513 cmd.drawIndexedIndirect.buffer = static_cast<const TestGraphicsBuffer*>(&buffer);
514 cmd.drawIndexedIndirect.offset = offset;
515 cmd.drawIndexedIndirect.drawCount = drawCount;
516 cmd.drawIndexedIndirect.stride = stride;
517 mCallStack.PushCall("DrawIndexedIndirect", "");
520 void Reset() override
523 mCallStack.PushCall("Reset", "");
526 void SetScissor(Graphics::Extent2D value) override
528 mCallStack.PushCall("SetScissor", "");
531 void SetScissorTestEnable(bool value) override
533 mCallStack.PushCall("SetScissorTestEnable", "");
536 void SetViewport(Graphics::Viewport value) override
538 mCallStack.PushCall("SetViewport", "");
541 void SetViewportEnable(bool value) override
543 mCallStack.PushCall("SetViewportEnable", "");
546 [[nodiscard]] const std::vector<Command>& GetCommands() const
552 * Returns number of draw commands
555 int GetDrawCallsCount();
558 * Retrieves state resolve for selected draw call
559 * @param drawCommandIndex
561 void GetStateForDrawCall( int drawCallIndex );
564 * Retrieves commands of specified type
566 std::vector<Command*> GetCommandsByType( CommandTypeMask mask );
570 TraceCallStack& mCallStack;
571 TestGlAbstraction& mGlAbstraction;
572 std::vector<Command> mCommands;
577 #endif //DALI_TEST_GRAPHICS_COMMAND_BUFFER_H