Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
Stephen White | ef2dc90 | 2019-08-26 15:19:42 -0400 | [diff] [blame] | 8 | #include "src/gpu/dawn/GrDawnOpsRenderPass.h" |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 9 | |
| 10 | #include "src/gpu/GrFixedClip.h" |
| 11 | #include "src/gpu/GrMesh.h" |
| 12 | #include "src/gpu/GrOpFlushState.h" |
| 13 | #include "src/gpu/GrPipeline.h" |
| 14 | #include "src/gpu/GrRenderTargetPriv.h" |
| 15 | #include "src/gpu/GrTexturePriv.h" |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 16 | #include "src/gpu/dawn/GrDawnBuffer.h" |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 17 | #include "src/gpu/dawn/GrDawnGpu.h" |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 18 | #include "src/gpu/dawn/GrDawnProgramBuilder.h" |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 19 | #include "src/gpu/dawn/GrDawnRenderTarget.h" |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 20 | #include "src/gpu/dawn/GrDawnStencilAttachment.h" |
Stephen White | e3b1389 | 2019-08-14 15:48:02 -0400 | [diff] [blame] | 21 | #include "src/gpu/dawn/GrDawnTexture.h" |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 22 | #include "src/gpu/dawn/GrDawnUtil.h" |
| 23 | #include "src/sksl/SkSLCompiler.h" |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 24 | |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 25 | //////////////////////////////////////////////////////////////////////////////// |
| 26 | |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 27 | static dawn::LoadOp to_dawn_load_op(GrLoadOp loadOp) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 28 | switch (loadOp) { |
| 29 | case GrLoadOp::kLoad: |
| 30 | return dawn::LoadOp::Load; |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 31 | case GrLoadOp::kDiscard: |
| 32 | // Use LoadOp::Load to emulate DontCare. |
| 33 | // Dawn doesn't have DontCare, for security reasons. |
| 34 | // Load should be equivalent to DontCare for desktop; Clear would |
| 35 | // probably be better for tilers. If Dawn does add DontCare |
| 36 | // as an extension, use it here. |
| 37 | return dawn::LoadOp::Load; |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 38 | case GrLoadOp::kClear: |
| 39 | return dawn::LoadOp::Clear; |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 40 | default: |
| 41 | SK_ABORT("Invalid LoadOp"); |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
Stephen White | ef2dc90 | 2019-08-26 15:19:42 -0400 | [diff] [blame] | 45 | GrDawnOpsRenderPass::GrDawnOpsRenderPass(GrDawnGpu* gpu, GrRenderTarget* rt, GrSurfaceOrigin origin, |
| 46 | const LoadAndStoreInfo& colorInfo, |
| 47 | const StencilLoadAndStoreInfo& stencilInfo) |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 48 | : INHERITED(rt, origin) |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 49 | , fGpu(gpu) |
| 50 | , fColorInfo(colorInfo) { |
| 51 | fEncoder = fGpu->device().CreateCommandEncoder(); |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 52 | dawn::LoadOp colorOp = to_dawn_load_op(colorInfo.fLoadOp); |
| 53 | dawn::LoadOp stencilOp = to_dawn_load_op(stencilInfo.fLoadOp); |
| 54 | fPassEncoder = beginRenderPass(colorOp, stencilOp); |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 55 | } |
| 56 | |
Stephen White | ef2dc90 | 2019-08-26 15:19:42 -0400 | [diff] [blame] | 57 | dawn::RenderPassEncoder GrDawnOpsRenderPass::beginRenderPass(dawn::LoadOp colorOp, |
| 58 | dawn::LoadOp stencilOp) { |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 59 | dawn::Texture texture = static_cast<GrDawnRenderTarget*>(fRenderTarget)->texture(); |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 60 | auto stencilAttachment = static_cast<GrDawnStencilAttachment*>( |
| 61 | fRenderTarget->renderTargetPriv().getStencilAttachment()); |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 62 | dawn::TextureView colorView = texture.CreateDefaultView(); |
| 63 | const float *c = fColorInfo.fClearColor.vec(); |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 64 | |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 65 | dawn::RenderPassColorAttachmentDescriptor colorAttachment; |
| 66 | colorAttachment.attachment = colorView; |
| 67 | colorAttachment.resolveTarget = nullptr; |
| 68 | colorAttachment.clearColor = { c[0], c[1], c[2], c[3] }; |
| 69 | colorAttachment.loadOp = colorOp; |
| 70 | colorAttachment.storeOp = dawn::StoreOp::Store; |
| 71 | dawn::RenderPassColorAttachmentDescriptor* colorAttachments = { &colorAttachment }; |
| 72 | dawn::RenderPassDescriptor renderPassDescriptor; |
| 73 | renderPassDescriptor.colorAttachmentCount = 1; |
| 74 | renderPassDescriptor.colorAttachments = &colorAttachments; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 75 | if (stencilAttachment) { |
| 76 | dawn::RenderPassDepthStencilAttachmentDescriptor depthStencilAttachment; |
| 77 | depthStencilAttachment.attachment = stencilAttachment->view(); |
| 78 | depthStencilAttachment.depthLoadOp = stencilOp; |
| 79 | depthStencilAttachment.stencilLoadOp = stencilOp; |
| 80 | depthStencilAttachment.clearDepth = 1.0f; |
| 81 | depthStencilAttachment.clearStencil = 0; |
| 82 | depthStencilAttachment.depthStoreOp = dawn::StoreOp::Store; |
| 83 | depthStencilAttachment.stencilStoreOp = dawn::StoreOp::Store; |
| 84 | renderPassDescriptor.depthStencilAttachment = &depthStencilAttachment; |
| 85 | } else { |
| 86 | renderPassDescriptor.depthStencilAttachment = nullptr; |
| 87 | } |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 88 | return fEncoder.BeginRenderPass(&renderPassDescriptor); |
| 89 | } |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 90 | |
Stephen White | ef2dc90 | 2019-08-26 15:19:42 -0400 | [diff] [blame] | 91 | GrDawnOpsRenderPass::~GrDawnOpsRenderPass() { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 92 | } |
| 93 | |
Stephen White | ef2dc90 | 2019-08-26 15:19:42 -0400 | [diff] [blame] | 94 | GrGpu* GrDawnOpsRenderPass::gpu() { return fGpu; } |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 95 | |
Stephen White | ef2dc90 | 2019-08-26 15:19:42 -0400 | [diff] [blame] | 96 | void GrDawnOpsRenderPass::end() { |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 97 | fPassEncoder.EndPass(); |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 98 | } |
| 99 | |
Stephen White | ef2dc90 | 2019-08-26 15:19:42 -0400 | [diff] [blame] | 100 | void GrDawnOpsRenderPass::submit() { |
Stephen White | d729e2d | 2019-08-28 12:37:47 -0400 | [diff] [blame] | 101 | fGpu->appendCommandBuffer(fEncoder.Finish()); |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 102 | } |
| 103 | |
Stephen White | ef2dc90 | 2019-08-26 15:19:42 -0400 | [diff] [blame] | 104 | void GrDawnOpsRenderPass::insertEventMarker(const char* msg) { |
Stephen White | c0a837f | 2019-07-30 11:38:22 -0400 | [diff] [blame] | 105 | SkASSERT(!"unimplemented"); |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 106 | } |
| 107 | |
Stephen White | ef2dc90 | 2019-08-26 15:19:42 -0400 | [diff] [blame] | 108 | void GrDawnOpsRenderPass::onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) { |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 109 | fPassEncoder.EndPass(); |
| 110 | fPassEncoder = beginRenderPass(dawn::LoadOp::Load, dawn::LoadOp::Clear); |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 111 | } |
| 112 | |
Stephen White | ef2dc90 | 2019-08-26 15:19:42 -0400 | [diff] [blame] | 113 | void GrDawnOpsRenderPass::onClear(const GrFixedClip& clip, const SkPMColor4f& color) { |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 114 | fPassEncoder.EndPass(); |
| 115 | fPassEncoder = beginRenderPass(dawn::LoadOp::Clear, dawn::LoadOp::Load); |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | //////////////////////////////////////////////////////////////////////////////// |
| 119 | |
Stephen White | ef2dc90 | 2019-08-26 15:19:42 -0400 | [diff] [blame] | 120 | void GrDawnOpsRenderPass::inlineUpload(GrOpFlushState* state, |
| 121 | GrDeferredTextureUploadFn& upload) { |
Stephen White | c0a837f | 2019-07-30 11:38:22 -0400 | [diff] [blame] | 122 | SkASSERT(!"unimplemented"); |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 125 | //////////////////////////////////////////////////////////////////////////////// |
| 126 | |
Stephen White | ef2dc90 | 2019-08-26 15:19:42 -0400 | [diff] [blame] | 127 | void GrDawnOpsRenderPass::setScissorState( |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 128 | const GrPipeline& pipeline, |
| 129 | const GrPipeline::FixedDynamicState* fixedDynamicState, |
| 130 | const GrPipeline::DynamicStateArrays* dynamicStateArrays) { |
| 131 | SkIRect rect; |
| 132 | if (pipeline.isScissorEnabled()) { |
| 133 | constexpr SkIRect kBogusScissor{0, 0, 1, 1}; |
| 134 | rect = fixedDynamicState ? fixedDynamicState->fScissorRect : kBogusScissor; |
| 135 | if (kBottomLeft_GrSurfaceOrigin == fOrigin) { |
| 136 | rect.setXYWH(rect.x(), fRenderTarget->height() - rect.bottom(), |
| 137 | rect.width(), rect.height()); |
| 138 | } |
| 139 | } else { |
| 140 | rect = SkIRect::MakeWH(fRenderTarget->width(), fRenderTarget->height()); |
| 141 | } |
| 142 | fPassEncoder.SetScissorRect(rect.x(), rect.y(), rect.width(), rect.height()); |
| 143 | } |
| 144 | |
Stephen White | ef2dc90 | 2019-08-26 15:19:42 -0400 | [diff] [blame] | 145 | void GrDawnOpsRenderPass::applyState(const GrPipeline& pipeline, |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 146 | const GrPrimitiveProcessor& primProc, |
| 147 | const GrTextureProxy* const primProcProxies[], |
| 148 | const GrPipeline::FixedDynamicState* fixedDynamicState, |
| 149 | const GrPipeline::DynamicStateArrays* dynamicStateArrays, |
| 150 | const GrPrimitiveType primitiveType, |
| 151 | bool hasPoints) { |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 152 | sk_sp<GrDawnProgram> program = fGpu->getOrCreateRenderPipeline(fRenderTarget, |
| 153 | fOrigin, |
| 154 | pipeline, |
| 155 | primProc, |
| 156 | primProcProxies, |
| 157 | hasPoints, |
| 158 | primitiveType); |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 159 | auto bindGroup = program->setData(fGpu, fRenderTarget, fOrigin, primProc, pipeline, |
| 160 | primProcProxies); |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 161 | fPassEncoder.SetPipeline(program->fRenderPipeline); |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 162 | fPassEncoder.SetBindGroup(0, bindGroup, 0, nullptr); |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 163 | if (pipeline.isStencilEnabled()) { |
| 164 | fPassEncoder.SetStencilReference(pipeline.getUserStencil()->fFront.fRef); |
| 165 | } |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 166 | GrXferProcessor::BlendInfo blendInfo = pipeline.getXferProcessor().getBlendInfo(); |
| 167 | const float* c = blendInfo.fBlendConstant.vec(); |
| 168 | dawn::Color color{c[0], c[1], c[2], c[3]}; |
| 169 | fPassEncoder.SetBlendColor(&color); |
| 170 | this->setScissorState(pipeline, fixedDynamicState, dynamicStateArrays); |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 171 | } |
| 172 | |
Stephen White | ef2dc90 | 2019-08-26 15:19:42 -0400 | [diff] [blame] | 173 | void GrDawnOpsRenderPass::onDraw(const GrPrimitiveProcessor& primProc, |
| 174 | const GrPipeline& pipeline, |
| 175 | const GrPipeline::FixedDynamicState* fixedDynamicState, |
| 176 | const GrPipeline::DynamicStateArrays* dynamicStateArrays, |
| 177 | const GrMesh meshes[], |
| 178 | int meshCount, |
| 179 | const SkRect& bounds) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 180 | if (!meshCount) { |
| 181 | return; |
| 182 | } |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 183 | bool hasPoints = false; |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 184 | for (int i = 0; i < meshCount; ++i) { |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 185 | if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) { |
| 186 | hasPoints = true; |
| 187 | } |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 188 | } |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 189 | const GrTextureProxy* const* primProcProxies = nullptr; |
| 190 | if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) { |
| 191 | primProcProxies = dynamicStateArrays->fPrimitiveProcessorTextures; |
| 192 | } else if (fixedDynamicState) { |
| 193 | primProcProxies = fixedDynamicState->fPrimitiveProcessorTextures; |
| 194 | } |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 195 | for (int i = 0; i < meshCount; ++i) { |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 196 | applyState(pipeline, primProc, primProcProxies, fixedDynamicState, dynamicStateArrays, |
| 197 | meshes[0].primitiveType(), hasPoints); |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 198 | meshes[i].sendToGpu(this); |
| 199 | } |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 200 | } |
| 201 | |
Stephen White | ef2dc90 | 2019-08-26 15:19:42 -0400 | [diff] [blame] | 202 | void GrDawnOpsRenderPass::sendInstancedMeshToGpu(GrPrimitiveType, |
| 203 | const GrBuffer* vertexBuffer, |
| 204 | int vertexCount, |
| 205 | int baseVertex, |
| 206 | const GrBuffer* instanceBuffer, |
| 207 | int instanceCount, |
| 208 | int baseInstance) { |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 209 | static const uint64_t vertexBufferOffsets[1] = {0}; |
| 210 | dawn::Buffer vb = static_cast<const GrDawnBuffer*>(vertexBuffer)->get(); |
| 211 | fPassEncoder.SetVertexBuffers(0, 1, &vb, vertexBufferOffsets); |
| 212 | fPassEncoder.Draw(vertexCount, 1, baseVertex, baseInstance); |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 213 | fGpu->stats()->incNumDraws(); |
| 214 | } |
| 215 | |
Stephen White | ef2dc90 | 2019-08-26 15:19:42 -0400 | [diff] [blame] | 216 | void GrDawnOpsRenderPass::sendIndexedInstancedMeshToGpu(GrPrimitiveType, |
| 217 | const GrBuffer* indexBuffer, |
| 218 | int indexCount, |
| 219 | int baseIndex, |
| 220 | const GrBuffer* vertexBuffer, |
| 221 | int baseVertex, |
| 222 | const GrBuffer* instanceBuffer, |
| 223 | int instanceCount, |
| 224 | int baseInstance, |
| 225 | GrPrimitiveRestart restart) { |
Stephen White | d67c71f | 2019-08-06 11:15:41 -0400 | [diff] [blame] | 226 | uint64_t vertexBufferOffsets[1]; |
| 227 | vertexBufferOffsets[0] = 0; |
| 228 | dawn::Buffer vb = static_cast<const GrDawnBuffer*>(vertexBuffer)->get(); |
| 229 | dawn::Buffer ib = static_cast<const GrDawnBuffer*>(indexBuffer)->get(); |
| 230 | fPassEncoder.SetIndexBuffer(ib, 0); |
| 231 | fPassEncoder.SetVertexBuffers(0, 1, &vb, vertexBufferOffsets); |
| 232 | fPassEncoder.DrawIndexed(indexCount, 1, baseIndex, baseVertex, baseInstance); |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 233 | fGpu->stats()->incNumDraws(); |
| 234 | } |