1 // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef VK_COMMAND_BUFFER_HPP_
16 #define VK_COMMAND_BUFFER_HPP_
17
18 #include "VkConfig.h"
19 #include "VkObject.hpp"
20 #include <memory>
21 #include <vector>
22
23 namespace sw
24 {
25 class Renderer;
26 }
27
28 namespace vk
29 {
30
31 class Framebuffer;
32 class Pipeline;
33 class RenderPass;
34
35 class CommandBuffer
36 {
37 public:
GetAllocationScope()38 static constexpr VkSystemAllocationScope GetAllocationScope() { return VK_SYSTEM_ALLOCATION_SCOPE_OBJECT; }
39
40 CommandBuffer(VkCommandBufferLevel pLevel);
41
42 void destroy(const VkAllocationCallbacks* pAllocator);
43
44 VkResult begin(VkCommandBufferUsageFlags flags, const VkCommandBufferInheritanceInfo* pInheritanceInfo);
45 VkResult end();
46 VkResult reset(VkCommandPoolResetFlags flags);
47
48 void beginRenderPass(VkRenderPass renderPass, VkFramebuffer framebuffer, VkRect2D renderArea,
49 uint32_t clearValueCount, const VkClearValue* pClearValues, VkSubpassContents contents);
50 void nextSubpass(VkSubpassContents contents);
51 void endRenderPass();
52 void executeCommands(uint32_t commandBufferCount, const VkCommandBuffer* pCommandBuffers);
53
54 void setDeviceMask(uint32_t deviceMask);
55 void dispatchBase(uint32_t baseGroupX, uint32_t baseGroupY, uint32_t baseGroupZ,
56 uint32_t groupCountX, uint32_t groupCountY, uint32_t groupCountZ);
57
58 void pipelineBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
59 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
60 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier* pBufferMemoryBarriers,
61 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier* pImageMemoryBarriers);
62 void bindPipeline(VkPipelineBindPoint pipelineBindPoint, VkPipeline pipeline);
63 void bindVertexBuffers(uint32_t firstBinding, uint32_t bindingCount,
64 const VkBuffer* pBuffers, const VkDeviceSize* pOffsets);
65
66 void beginQuery(VkQueryPool queryPool, uint32_t query, VkQueryControlFlags flags);
67 void endQuery(VkQueryPool queryPool, uint32_t query);
68 void resetQueryPool(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount);
69 void writeTimestamp(VkPipelineStageFlagBits pipelineStage, VkQueryPool queryPool, uint32_t query);
70 void copyQueryPoolResults(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount,
71 VkBuffer dstBuffer, VkDeviceSize dstOffset, VkDeviceSize stride, VkQueryResultFlags flags);
72 void pushConstants(VkPipelineLayout layout, VkShaderStageFlags stageFlags,
73 uint32_t offset, uint32_t size, const void* pValues);
74
75 void setViewport(uint32_t firstViewport, uint32_t viewportCount, const VkViewport* pViewports);
76 void setScissor(uint32_t firstScissor, uint32_t scissorCount, const VkRect2D* pScissors);
77 void setLineWidth(float lineWidth);
78 void setDepthBias(float depthBiasConstantFactor, float depthBiasClamp, float depthBiasSlopeFactor);
79 void setBlendConstants(const float blendConstants[4]);
80 void setDepthBounds(float minDepthBounds, float maxDepthBounds);
81 void setStencilCompareMask(VkStencilFaceFlags faceMask, uint32_t compareMask);
82 void setStencilWriteMask(VkStencilFaceFlags faceMask, uint32_t writeMask);
83 void setStencilReference(VkStencilFaceFlags faceMask, uint32_t reference);
84 void bindDescriptorSets(VkPipelineBindPoint pipelineBindPoint, VkPipelineLayout layout,
85 uint32_t firstSet, uint32_t descriptorSetCount, const VkDescriptorSet* pDescriptorSets,
86 uint32_t dynamicOffsetCount, const uint32_t* pDynamicOffsets);
87 void bindIndexBuffer(VkBuffer buffer, VkDeviceSize offset, VkIndexType indexType);
88 void dispatch(uint32_t groupCountX, uint32_t groupCountY, uint32_t groupCountZ);
89 void dispatchIndirect(VkBuffer buffer, VkDeviceSize offset);
90 void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, uint32_t regionCount, const VkBufferCopy* pRegions);
91 void copyImage(VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage, VkImageLayout dstImageLayout,
92 uint32_t regionCount, const VkImageCopy* pRegions);
93 void blitImage(VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage, VkImageLayout dstImageLayout,
94 uint32_t regionCount, const VkImageBlit* pRegions, VkFilter filter);
95 void copyBufferToImage(VkBuffer srcBuffer, VkImage dstImage, VkImageLayout dstImageLayout,
96 uint32_t regionCount, const VkBufferImageCopy* pRegions);
97 void copyImageToBuffer(VkImage srcImage, VkImageLayout srcImageLayout, VkBuffer dstBuffer,
98 uint32_t regionCount, const VkBufferImageCopy* pRegions);
99 void updateBuffer(VkBuffer dstBuffer, VkDeviceSize dstOffset, VkDeviceSize dataSize, const void* pData);
100 void fillBuffer(VkBuffer dstBuffer, VkDeviceSize dstOffset, VkDeviceSize size, uint32_t data);
101 void clearColorImage(VkImage image, VkImageLayout imageLayout, const VkClearColorValue* pColor,
102 uint32_t rangeCount, const VkImageSubresourceRange* pRanges);
103 void clearDepthStencilImage(VkImage image, VkImageLayout imageLayout, const VkClearDepthStencilValue* pDepthStencil,
104 uint32_t rangeCount, const VkImageSubresourceRange* pRanges);
105 void clearAttachments(uint32_t attachmentCount, const VkClearAttachment* pAttachments,
106 uint32_t rectCount, const VkClearRect* pRects);
107 void resolveImage(VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage, VkImageLayout dstImageLayout,
108 uint32_t regionCount, const VkImageResolve* pRegions);
109 void setEvent(VkEvent event, VkPipelineStageFlags stageMask);
110 void resetEvent(VkEvent event, VkPipelineStageFlags stageMask);
111 void waitEvents(uint32_t eventCount, const VkEvent* pEvents, VkPipelineStageFlags srcStageMask,
112 VkPipelineStageFlags dstStageMask, uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
113 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier* pBufferMemoryBarriers,
114 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier* pImageMemoryBarriers);
115
116 void draw(uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance);
117 void drawIndexed(uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance);
118 void drawIndirect(VkBuffer buffer, VkDeviceSize offset, uint32_t drawCount, uint32_t stride);
119 void drawIndexedIndirect(VkBuffer buffer, VkDeviceSize offset, uint32_t drawCount, uint32_t stride);
120
121 // TODO(sugoi): Move ExecutionState out of CommandBuffer (possibly into Device)
122 struct ExecutionState
123 {
124 sw::Renderer* renderer = nullptr;
125 RenderPass* renderPass = nullptr;
126 Framebuffer* renderPassFramebuffer = nullptr;
127 Pipeline* pipelines[VK_PIPELINE_BIND_POINT_RANGE_SIZE] = {};
128
129 struct VertexInputBinding
130 {
131 VkBuffer buffer;
132 VkDeviceSize offset;
133 };
134 VertexInputBinding vertexInputBindings[MAX_VERTEX_INPUT_BINDINGS] = {};
135 };
136
137 void submit(CommandBuffer::ExecutionState& executionState);
138
139 class Command;
140 private:
141 void resetState();
142 template<typename T, typename... Args> void addCommand(Args&&... args);
143
144 enum State { INITIAL, RECORDING, EXECUTABLE, PENDING, INVALID };
145 State state = INITIAL;
146 VkCommandBufferLevel level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
147
148 // FIXME (b/119409619): replace this vector by an allocator so we can control all memory allocations
149 std::vector<std::unique_ptr<Command>>* commands;
150 };
151
152 using DispatchableCommandBuffer = DispatchableObject<CommandBuffer, VkCommandBuffer>;
153
Cast(VkCommandBuffer object)154 static inline CommandBuffer* Cast(VkCommandBuffer object)
155 {
156 return DispatchableCommandBuffer::Cast(object);
157 }
158
159 } // namespace vk
160
161 #endif // VK_COMMAND_BUFFER_HPP_
162