Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.92 KB | None | 0 0
  1. // Copyright (C) 2019 Arthur LAURENT <arthur.laurent4@gmail.com>
  2. // This file is subject to the license terms in the LICENSE file
  3. // found in the top-level of this distribution
  4.  
  5. #pragma once
  6.  
  7. #include <storm/core/Platform.hpp>
  8.  
  9. #ifdef STORM_COMPILER_MSVC
  10. #pragma warning(push)
  11. #pragma warning(disable:4251)
  12. #endif
  13.  
  14. #include <gsl/string_span>
  15. #include <queue>
  16. #include <storm/core/NonCopyable.hpp>
  17. #include <storm/render/core/Command.hpp>
  18. #include <storm/render/core/Enums.hpp>
  19. #include <storm/render/core/Fwd.hpp>
  20. #include <storm/render/pipeline/GraphicsPipeline.hpp>
  21.  
  22. namespace storm::render {
  23.     class STORM_PUBLIC CommandBuffer : public core::NonCopyable {
  24.     public:
  25.         enum class State { Initial, Recording, Executable };
  26.  
  27.         virtual ~CommandBuffer() = 0;
  28.  
  29.         CommandBuffer(CommandBuffer &&) ;
  30.         CommandBuffer &operator=(CommandBuffer &&) ;
  31.  
  32.         virtual void reset() noexcept = 0;
  33.         virtual void build() noexcept = 0;
  34.  
  35.         inline State state() const noexcept { return m_state; }
  36.  
  37.         template<typename CommandT>
  38.         inline void add(CommandT &&command) {
  39.             Expects(m_state == State::Initial || m_state == State::Recording);
  40.  
  41.             if(m_state == State::Initial) m_state = State::Recording;
  42.  
  43.             m_commands.emplace(std::forward<CommandT>(command));
  44.         }
  45.  
  46.         template<typename CommandT, typename... Args>
  47.         inline void add(Args &&... args) {
  48.             Expects(m_state == State::Initial || m_state == State::Recording);
  49.  
  50.             if(m_state == State::Initial) m_state = State::Recording;
  51.  
  52.             m_commands.emplace(CommandT{std::forward<Args>(args)...});
  53.         }
  54.  
  55.         inline void begin(
  56.           bool one_time_submit                    = false,
  57.           std::optional<CommandBufferCRef> parent = std::nullopt) {
  58.             add<BeginCommand>(one_time_submit, parent);
  59.         }
  60.  
  61.         inline void end() { add<EndCommand>(); }
  62.  
  63.         inline void beginRenderPass(
  64.           const RenderPass &render_pass,
  65.           const Framebuffer &framebuffer,
  66.           core::RGBColorF clear_color = core::RGBColorDef::Black,
  67.           float depth_color           = 1.f,
  68.           std::uint32_t stencil_value = 0u) {
  69.             add<BeginRenderPassCommand>(
  70.               render_pass, framebuffer, std::move(clear_color), depth_color, stencil_value);
  71.         }
  72.  
  73.         inline void endRenderPass() { add<EndRenderPassCommand>(); }
  74.  
  75.         inline void bindGraphicsPipeline(const GraphicsPipeline &pipeline) {
  76.             add<BindGraphicsPipelineCommand>(pipeline);
  77.         }
  78.  
  79.         inline void draw(
  80.           std::uint32_t vertex_count,
  81.           std::uint32_t instance_count = 1u,
  82.           std::uint32_t first_vertex   = 0,
  83.           std::uint32_t first_instance = 0) {
  84.             add<DrawCommand>(vertex_count, instance_count, first_vertex, first_instance);
  85.         }
  86.  
  87.         inline void drawIndexed(
  88.           std::uint32_t index_count,
  89.           std::uint32_t instance_count = 1u,
  90.           std::uint32_t first_index    = 0u,
  91.           std::int32_t vertex_offset   = 0,
  92.           std::uint32_t first_instance = 0u) {
  93.             add<DrawIndexedCommand>(
  94.               index_count, instance_count, first_index, vertex_offset, first_instance);
  95.         }
  96.  
  97.         inline void bindVertexBuffers(
  98.           std::vector<HardwareBufferCRef> buffers,
  99.           std::vector<std::ptrdiff_t> offsets) {
  100.             add<BindVertexBuffersCommand>(std::move(buffers), std::move(offsets));
  101.         }
  102.  
  103.         inline void bindIndexBuffer(
  104.           const HardwareBuffer &buffer,
  105.           std::ptrdiff_t offset = 0,
  106.           bool large_indices    = false) {
  107.             add<BindIndexBufferCommand>(buffer, offset, large_indices);
  108.         }
  109.  
  110.         inline void copyBuffer(
  111.           const HardwareBuffer &source,
  112.           const HardwareBuffer &destination,
  113.           std::size_t size,
  114.           std::ptrdiff_t src_offset = 0u,
  115.           std::ptrdiff_t dst_offset = 0u) {
  116.             add<CopyBufferCommand>(source, destination, size, src_offset, dst_offset);
  117.         }
  118.  
  119.         inline void bindDescriptorSets(
  120.           GraphicsPipeline &pipeline,
  121.           std::vector<DescriptorSetCRef> descriptor_sets) {
  122.             add<BindDescriptorSetsCommand>(&pipeline, std::move(descriptor_sets));
  123.         }
  124.  
  125.         inline void copyBufferToImage(const HardwareBuffer &source, const Texture &destination) {
  126.             add<CopyBufferToImageCommand>(source, destination);
  127.         }
  128.  
  129.         inline void transitionImageLayout(Texture &image, ImageLayout new_layout) {
  130.             add<TransitionImageLayoutCommand>(image, new_layout);
  131.         }
  132.  
  133.     protected:
  134.         explicit CommandBuffer(const Queue &queue);
  135.  
  136.         State m_state = State::Initial;
  137.         QueueCRef m_queue;
  138.  
  139.         std::queue<Command> m_commands;
  140.     };
  141. } // namespace storm::render
  142.  
  143. #ifdef STORM_COMPILER_MSVC
  144. #pragma warning(pop)
  145. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement