| 1 |
/* |
| 2 |
* Copyright 2022 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef skgpu_graphite_VulkanCommandBuffer_DEFINED |
| 9 |
#define skgpu_graphite_VulkanCommandBuffer_DEFINED |
| 10 |
|
| 11 |
#include "src/gpu/graphite/CommandBuffer.h" |
| 12 |
|
| 13 |
#include "include/gpu/vk/VulkanTypes.h" |
| 14 |
#include "src/gpu/graphite/DrawPass.h" |
| 15 |
#include "src/gpu/graphite/vk/VulkanGraphicsPipeline.h" |
| 16 |
#include "src/gpu/graphite/vk/VulkanResourceProvider.h" |
| 17 |
|
| 18 |
namespace skgpu::graphite { |
| 19 |
|
| 20 |
class Buffer; |
| 21 |
class VulkanBuffer; |
| 22 |
class VulkanDescriptorSet; |
| 23 |
class VulkanSharedContext; |
| 24 |
class VulkanTexture; |
| 25 |
|
| 26 |
class VulkanCommandBuffer final : public CommandBuffer { |
| 27 |
public: |
| 28 |
static std::unique_ptr<VulkanCommandBuffer> Make(const VulkanSharedContext*, |
| 29 |
VulkanResourceProvider*, |
| 30 |
Protected); |
| 31 |
~VulkanCommandBuffer() override; |
| 32 |
|
| 33 |
bool setNewCommandBufferResources() override; |
| 34 |
|
| 35 |
bool submit(VkQueue, const SubmitInfo&); |
| 36 |
|
| 37 |
bool isFinished(); |
| 38 |
|
| 39 |
void waitUntilFinished(); |
| 40 |
|
| 41 |
void addBufferMemoryBarrier(const Resource* resource, |
| 42 |
VkPipelineStageFlags srcStageMask, |
| 43 |
VkPipelineStageFlags dstStageMask, |
| 44 |
VkBufferMemoryBarrier* barrier); |
| 45 |
void addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 46 |
VkPipelineStageFlags dstStageMask, |
| 47 |
VkBufferMemoryBarrier* barrier); |
| 48 |
void addImageMemoryBarrier(const Resource*, |
| 49 |
VkPipelineStageFlags srcStageMask, |
| 50 |
VkPipelineStageFlags dstStageMask, |
| 51 |
bool byRegion, |
| 52 |
VkImageMemoryBarrier* barrier); |
| 53 |
|
| 54 |
private: |
| 55 |
VulkanCommandBuffer(VkCommandPool pool, |
| 56 |
VkCommandBuffer primaryCommandBuffer, |
| 57 |
const VulkanSharedContext* sharedContext, |
| 58 |
VulkanResourceProvider* resourceProvider, |
| 59 |
Protected); |
| 60 |
|
| 61 |
ResourceProvider* resourceProvider() const override { return fResourceProvider; } |
| 62 |
|
| 63 |
void onResetCommandBuffer() override; |
| 64 |
|
| 65 |
void begin(); |
| 66 |
void end(); |
| 67 |
|
| 68 |
void addWaitSemaphores(size_t numWaitSemaphores, |
| 69 |
const BackendSemaphore* waitSemaphores) override; |
| 70 |
void addSignalSemaphores(size_t numWaitSemaphores, |
| 71 |
const BackendSemaphore* signalSemaphores) override; |
| 72 |
void prepareSurfaceForStateUpdate(SkSurface* targetSurface, |
| 73 |
const MutableTextureState* newState) override; |
| 74 |
|
| 75 |
const Sampler* getSampler(const DrawPassCommands::BindTexturesAndSamplers* command, |
| 76 |
int32_t index); |
| 77 |
|
| 78 |
bool onAddRenderPass(const RenderPassDesc&, |
| 79 |
SkIRect renderPassBounds, |
| 80 |
const Texture* colorTexture, |
| 81 |
const Texture* resolveTexture, |
| 82 |
const Texture* depthStencilTexture, |
| 83 |
SkIPoint resolveOffset, |
| 84 |
SkIRect viewport, |
| 85 |
const DrawPassList&) override; |
| 86 |
|
| 87 |
bool beginRenderPass(const RenderPassDesc&, |
| 88 |
SkIRect renderPassBounds, |
| 89 |
const Texture* colorTexture, |
| 90 |
const Texture* resolveTexture, |
| 91 |
const Texture* depthStencilTexture); |
| 92 |
|
| 93 |
void performOncePerRPUpdates(SkIRect viewport, bool bindDstAsInputAttachment); |
| 94 |
|
| 95 |
void endRenderPass(); |
| 96 |
|
| 97 |
void addDrawPass(DrawPass*); |
| 98 |
|
| •99 |
// Track descriptor changes for binding prior to draw calls |
| 100 |
void recordBufferBindingInfo(const BindBufferInfo& info, UniformSlot); |
| 101 |
// Either both arguments are non-null, or both must be null (to reset or handle just the |
| 102 |
// dstCopy intrinsic w/o requiring a DrawPass command). |
| 103 |
void recordTextureAndSamplerDescSet( |
| 104 |
const DrawPass*, const DrawPassCommands::BindTexturesAndSamplers*); |
| 105 |
|
| 106 |
bool updateAndBindInputAttachment(const VulkanTexture&, const int setIdx, VkPipelineLayout); |
| 107 |
void bindTextureSamplers(); |
| 108 |
void bindUniformBuffers(); |
| 109 |
void syncDescriptorSets(); |
| 110 |
|
| 111 |
struct PushConstantInfo { |
| 112 |
uint32_t fOffset; |
| 113 |
uint32_t fSize; |
| 114 |
VkShaderStageFlagBits fShaderStageFlagBits; |
| 115 |
const void* fValues; |
| 116 |
}; |
| 117 |
void bindGraphicsPipeline(const GraphicsPipeline*); |
| 118 |
void pushConstants(const PushConstantInfo&, VkPipelineLayout compatibleLayout); |
| 119 |
|
| 120 |
void setBlendConstants(std::array<float, 4> blendConstants); |
| 121 |
void bindInputBuffer(const Buffer* buffer, VkDeviceSize offset, uint32_t binding); |
| 122 |
void bindIndexBuffer(const Buffer* indexBuffer, size_t offset); |
| 123 |
void bindIndirectBuffer(const Buffer* indirectBuffer, size_t offset); |
| 124 |
void setScissor(const Scissor&); |
| 125 |
void setScissor(const SkIRect&); |
| 126 |
|
| 127 |
void draw(PrimitiveType type, unsigned int baseVertex, unsigned int vertexCount); |
| 128 |
void drawIndexed(PrimitiveType type, unsigned int baseIndex, unsigned int indexCount, |
| 129 |
unsigned int baseVertex); |
| 130 |
void drawInstanced(PrimitiveType type, |
| 131 |
unsigned int baseVertex, unsigned int vertexCount, |
| 132 |
unsigned int baseInstance, unsigned int instanceCount); |
| 133 |
void drawIndexedInstanced(PrimitiveType type, unsigned int baseIndex, |
| 134 |
unsigned int indexCount, unsigned int baseVertex, |
| 135 |
unsigned int baseInstance, unsigned int instanceCount); |
| 136 |
void drawIndirect(PrimitiveType type); |
| 137 |
void drawIndexedIndirect(PrimitiveType type); |
| 138 |
void addBarrier(BarrierType type); |
| 139 |
|
| 140 |
// TODO: The virtuals in this class have not yet been implemented as we still haven't |
| 141 |
// implemented the objects they use. |
| 142 |
bool onAddComputePass(DispatchGroupSpan) override; |
| 143 |
|
| 144 |
bool onCopyBufferToBuffer(const Buffer* srcBuffer, |
| 145 |
size_t srcOffset, |
| 146 |
const Buffer* dstBuffer, |
| 147 |
size_t dstOffset, |
| 148 |
size_t size) override; |
| 149 |
bool onCopyTextureToBuffer(const Texture*, |
| 150 |
SkIRect srcRect, |
| 151 |
const Buffer*, |
| 152 |
size_t bufferOffset, |
| 153 |
size_t bufferRowBytes) override; |
| 154 |
bool onCopyBufferToTexture(const Buffer*, |
| 155 |
const Texture*, |
| 156 |
const BufferTextureCopyData* copyData, |
| 157 |
int count) override; |
| 158 |
bool onCopyTextureToTexture(const Texture* src, |
| 159 |
SkIRect srcRect, |
| 160 |
const Texture* dst, |
| 161 |
SkIPoint dstPoint, |
| 162 |
int mipLevel) override; |
| 163 |
|
| 164 |
bool pushConstants(VkShaderStageFlags stageFlags, |
| 165 |
uint32_t offset, |
| 166 |
uint32_t size, |
| 167 |
const void* values); |
| 168 |
|
| 169 |
bool onSynchronizeBufferToCpu(const Buffer*, bool* outDidResultInWork) override; |
| 170 |
bool onClearBuffer(const Buffer*, size_t offset, size_t size) override; |
| 171 |
|
| 172 |
enum PipelineBarrierType { |
| 173 |
kBufferMemory_BarrierType, |
| 174 |
kImageMemory_BarrierType |
| 175 |
}; |
| 176 |
void pipelineBarrier(const Resource* resource, |
| 177 |
VkPipelineStageFlags srcStageMask, |
| 178 |
VkPipelineStageFlags dstStageMask, |
| 179 |
bool byRegion, |
| 180 |
PipelineBarrierType barrierType, |
| 181 |
void* barrier); |
| 182 |
void submitPipelineBarriers(bool forSelfDependency = false); |
| 183 |
|
| 184 |
bool loadMSAAFromResolve(const RenderPassDesc&, |
| 185 |
VulkanTexture& resolveTexture, |
| 186 |
SkISize dstDimensions, |
| 187 |
SkIRect nativeBounds); |
| 188 |
void nextSubpass(); |
| 189 |
void setViewport(SkIRect viewport); |
| 190 |
|
| 191 |
VkCommandPool fPool; |
| 192 |
VkCommandBuffer fPrimaryCommandBuffer; |
| 193 |
const VulkanSharedContext* fSharedContext; |
| 194 |
VulkanResourceProvider* fResourceProvider; |
| 195 |
|
| 196 |
// begin() has been called, but not end() |
| 197 |
bool fActive = false; |
| 198 |
// Track whether there is currently an active render pass (beginRenderPass has been called, but |
| 199 |
// not endRenderPass) |
| 200 |
bool fActiveRenderPass = false; |
| 201 |
// Store a ptr to the active RenderPass's target texture so we have access to it for any |
| 202 |
// AddBarrier DrawPassCommands that pertain to the dst. A raw ptr is acceptable here because the |
| 203 |
// target texture is kept alive via a command buffer reference. |
| 204 |
VulkanTexture* fTargetTexture = nullptr; |
| 205 |
const VulkanGraphicsPipeline* fActiveGraphicsPipeline = nullptr; |
| 206 |
|
| 207 |
VkFence fSubmitFence = VK_NULL_HANDLE; |
| 208 |
|
| 209 |
// Current semaphores |
| 210 |
skia_private::STArray<1, VkSemaphore> fWaitSemaphores; |
| 211 |
skia_private::STArray<1, VkSemaphore> fSignalSemaphores; |
| 212 |
|
| 213 |
// Tracking of memory barriers so that we can submit them all in a batch together. |
| 214 |
skia_private::STArray<1, VkBufferMemoryBarrier> fBufferBarriers; |
| 215 |
skia_private::STArray<2, VkImageMemoryBarrier> fImageBarriers; |
| 216 |
bool fBarriersByRegion = false; |
| 217 |
VkPipelineStageFlags fSrcStageMask = 0; |
| 218 |
VkPipelineStageFlags fDstStageMask = 0; |
| 219 |
|
| 220 |
// Track whether certain descriptor sets need to be bound |
| 221 |
bool fBindUniformBuffers = false; |
| 222 |
bool fBindTextureSamplers = false; |
| 223 |
|
| 224 |
std::array<BindBufferInfo, VulkanGraphicsPipeline::kNumUniformBuffers> fUniformBuffersToBind; |
| 225 |
VkDescriptorSet fTextureSamplerDescSetToBind = VK_NULL_HANDLE; |
| 226 |
|
| 227 |
int fNumTextureSamplers = 0; |
| 228 |
|
| 229 |
// Tracking for whether an indirect buffer should be rebound. |
| 230 |
VkBuffer fBoundIndirectBuffer = VK_NULL_HANDLE; |
| 231 |
size_t fBoundIndirectBufferOffset = 0; |
| 232 |
|
| 233 |
std::array<float, 4> fCachedBlendConstant; |
| 234 |
}; |
| 235 |
|
| 236 |
} // namespace skgpu::graphite |
| 237 |
|
| 238 |
#endif // skgpu_graphite_VulkanCommandBuffer_DEFINED |