GrMtlGpuCommandBuffer.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright 2018 Google Inc.
  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. #ifndef GrMtlGpuCommandBuffer_DEFINED
  8. #define GrMtlGpuCommandBuffer_DEFINED
  9. #include "src/gpu/GrGpuCommandBuffer.h"
  10. #include "src/gpu/GrMesh.h"
  11. #include "src/gpu/GrOpFlushState.h"
  12. #include "src/gpu/mtl/GrMtlGpu.h"
  13. #import <Metal/Metal.h>
  14. typedef uint32_t GrColor;
  15. class GrMtlBuffer;
  16. class GrMtlPipelineState;
  17. class GrMtlRenderTarget;
  18. class GrMtlGpuTextureCommandBuffer : public GrGpuTextureCommandBuffer {
  19. public:
  20. GrMtlGpuTextureCommandBuffer(GrMtlGpu* gpu, GrTexture* texture, GrSurfaceOrigin origin)
  21. : INHERITED(texture, origin)
  22. , fGpu(gpu) {
  23. }
  24. ~GrMtlGpuTextureCommandBuffer() override {}
  25. void copy(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) override {
  26. fGpu->copySurface(fTexture, src, srcRect, dstPoint);
  27. }
  28. void transferFrom(const SkIRect& srcRect, GrColorType bufferColorType,
  29. GrGpuBuffer* transferBuffer, size_t offset) override {
  30. fGpu->transferPixelsFrom(fTexture, srcRect.fLeft, srcRect.fTop, srcRect.width(),
  31. srcRect.height(), bufferColorType, transferBuffer, offset);
  32. }
  33. void insertEventMarker(const char* msg) override {}
  34. private:
  35. GrMtlGpu* fGpu;
  36. typedef GrGpuTextureCommandBuffer INHERITED;
  37. };
  38. class GrMtlGpuRTCommandBuffer : public GrGpuRTCommandBuffer, private GrMesh::SendToGpuImpl {
  39. public:
  40. GrMtlGpuRTCommandBuffer(GrMtlGpu* gpu, GrRenderTarget* rt, GrSurfaceOrigin origin,
  41. const SkRect& bounds,
  42. const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
  43. const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo);
  44. ~GrMtlGpuRTCommandBuffer() override;
  45. void begin() override {}
  46. void end() override {}
  47. void insertEventMarker(const char* msg) override {}
  48. void initRenderState(id<MTLRenderCommandEncoder>);
  49. void inlineUpload(GrOpFlushState* state, GrDeferredTextureUploadFn& upload) override {
  50. // TODO: this could be more efficient
  51. state->doUpload(upload);
  52. }
  53. void transferFrom(const SkIRect& srcRect, GrColorType bufferColorType,
  54. GrGpuBuffer* transferBuffer, size_t offset) override;
  55. void copy(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) override;
  56. void submit();
  57. private:
  58. GrGpu* gpu() override { return fGpu; }
  59. GrMtlPipelineState* prepareDrawState(
  60. const GrPrimitiveProcessor& primProc,
  61. const GrPipeline& pipeline,
  62. const GrPipeline::FixedDynamicState* fixedDynamicState,
  63. GrPrimitiveType primType);
  64. void onDraw(const GrPrimitiveProcessor& primProc,
  65. const GrPipeline& pipeline,
  66. const GrPipeline::FixedDynamicState* fixedDynamicState,
  67. const GrPipeline::DynamicStateArrays* dynamicStateArrays,
  68. const GrMesh mesh[],
  69. int meshCount,
  70. const SkRect& bounds) override;
  71. void onClear(const GrFixedClip& clip, const SkPMColor4f& color) override;
  72. void onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) override;
  73. void setupRenderPass(const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
  74. const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo);
  75. void bindGeometry(const GrBuffer* vertexBuffer, size_t vertexOffset,
  76. const GrBuffer* instanceBuffer);
  77. // GrMesh::SendToGpuImpl methods. These issue the actual Metal draw commands.
  78. // Marked final as a hint to the compiler to not use virtual dispatch.
  79. void sendMeshToGpu(GrPrimitiveType primType, const GrBuffer* vertexBuffer, int vertexCount,
  80. int baseVertex) final;
  81. void sendIndexedMeshToGpu(GrPrimitiveType primType, const GrBuffer* indexBuffer, int indexCount,
  82. int baseIndex, uint16_t /*minIndexValue*/, uint16_t /*maxIndexValue*/,
  83. const GrBuffer* vertexBuffer, int baseVertex,
  84. GrPrimitiveRestart restart) final;
  85. void sendInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* vertexBuffer, int vertexCount,
  86. int baseVertex, const GrBuffer* instanceBuffer, int instanceCount,
  87. int baseInstance) final;
  88. void sendIndexedInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* indexBuffer, int indexCount,
  89. int baseIndex, const GrBuffer* vertexBuffer, int baseVertex,
  90. const GrBuffer* instanceBuffer, int instanceCount,
  91. int baseInstance, GrPrimitiveRestart) final;
  92. void setVertexBuffer(id<MTLRenderCommandEncoder>, const GrMtlBuffer*, size_t offset,
  93. size_t index);
  94. void resetBufferBindings();
  95. void precreateCmdEncoder();
  96. GrMtlGpu* fGpu;
  97. // GrRenderTargetProxy bounds
  98. #ifdef SK_DEBUG
  99. SkRect fRTBounds;
  100. #endif
  101. id<MTLRenderCommandEncoder> fActiveRenderCmdEncoder;
  102. MTLRenderPassDescriptor* fRenderPassDesc;
  103. SkRect fBounds;
  104. size_t fCurrentVertexStride;
  105. static constexpr size_t kNumBindings = GrMtlUniformHandler::kLastUniformBinding + 3;
  106. struct {
  107. id<MTLBuffer> fBuffer;
  108. size_t fOffset;
  109. } fBufferBindings[kNumBindings];
  110. typedef GrGpuRTCommandBuffer INHERITED;
  111. };
  112. #endif