blob: 020207e19ed14167a1da6aae22723dc2656c4ef7 [file] [log] [blame]
Stephen White985741a2019-07-18 11:43:45 -04001/*
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
Mike Klein52337de2019-07-25 09:00:52 -05008#include "src/gpu/dawn/GrDawnGpuCommandBuffer.h"
Stephen White985741a2019-07-18 11:43:45 -04009
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 Whited67c71f2019-08-06 11:15:41 -040016#include "src/gpu/dawn/GrDawnBuffer.h"
Stephen White985741a2019-07-18 11:43:45 -040017#include "src/gpu/dawn/GrDawnGpu.h"
Stephen Whited67c71f2019-08-06 11:15:41 -040018#include "src/gpu/dawn/GrDawnProgramBuilder.h"
Stephen White985741a2019-07-18 11:43:45 -040019#include "src/gpu/dawn/GrDawnRenderTarget.h"
Stephen Whitef813ef72019-08-09 12:28:37 -040020#include "src/gpu/dawn/GrDawnStencilAttachment.h"
Stephen Whitee3b13892019-08-14 15:48:02 -040021#include "src/gpu/dawn/GrDawnTexture.h"
Stephen Whited67c71f2019-08-06 11:15:41 -040022#include "src/gpu/dawn/GrDawnUtil.h"
23#include "src/sksl/SkSLCompiler.h"
Stephen White985741a2019-07-18 11:43:45 -040024
Stephen Whitee3b13892019-08-14 15:48:02 -040025GrDawnGpuTextureCommandBuffer::GrDawnGpuTextureCommandBuffer(GrDawnGpu* gpu,
26 GrTexture* texture,
27 GrSurfaceOrigin origin)
28 : INHERITED(texture, origin)
29 , fGpu(gpu) {
30 fEncoder = fGpu->device().CreateCommandEncoder();
Stephen White985741a2019-07-18 11:43:45 -040031}
32
Stephen Whitee3b13892019-08-14 15:48:02 -040033void GrDawnGpuTextureCommandBuffer::copy(GrSurface* src, const SkIRect& srcRect,
34 const SkIPoint& dstPoint) {
35 if (!src->asTexture()) {
36 return;
37 }
38 uint32_t width = srcRect.width(), height = srcRect.height();
39 size_t rowBytes = srcRect.width() * GrBytesPerPixel(src->config());
40 rowBytes = GrDawnRoundRowBytes(rowBytes);
41 size_t sizeInBytes = height * rowBytes;
42
43 dawn::BufferDescriptor desc;
44 desc.usage = dawn::BufferUsageBit::CopySrc | dawn::BufferUsageBit::CopyDst;
45 desc.size = sizeInBytes;
46
47 dawn::Buffer buffer = fGpu->device().CreateBuffer(&desc);
48
49 dawn::TextureCopyView srcTextureView, dstTextureView;
50 srcTextureView.texture = static_cast<GrDawnTexture*>(src->asTexture())->texture();
51 srcTextureView.origin = {(uint32_t) srcRect.x(), (uint32_t) srcRect.y(), 0};
52 dstTextureView.texture = static_cast<GrDawnTexture*>(fTexture)->texture();
53 dstTextureView.origin = {(uint32_t) dstPoint.x(), (uint32_t) dstPoint.y(), 0};
54
55 dawn::BufferCopyView bufferView;
56 bufferView.buffer = buffer;
57 bufferView.offset = 0;
58 bufferView.rowPitch = rowBytes;
59 bufferView.imageHeight = height;
60
61 dawn::Extent3D copySize = {width, height, 1};
62 fEncoder.CopyTextureToBuffer(&srcTextureView, &bufferView, &copySize);
63 fEncoder.CopyBufferToTexture(&bufferView, &dstTextureView, &copySize);
64}
65
66void GrDawnGpuTextureCommandBuffer::transferFrom(const SkIRect& srcRect,
67 GrColorType surfaceColorType,
68 GrColorType bufferColorType,
69 GrGpuBuffer* transferBuffer,
70 size_t offset) {
71 fGpu->transferPixelsFrom(fTexture, srcRect.fLeft, srcRect.fTop, srcRect.width(),
72 srcRect.height(), surfaceColorType, bufferColorType, transferBuffer,
73 offset);
Stephen White985741a2019-07-18 11:43:45 -040074}
75
76void GrDawnGpuTextureCommandBuffer::submit() {
Stephen Whitee3b13892019-08-14 15:48:02 -040077 dawn::CommandBuffer commandBuffer = fEncoder.Finish();
78 if (commandBuffer) {
79 fGpu->queue().Submit(1, &commandBuffer);
Stephen White985741a2019-07-18 11:43:45 -040080 }
81}
82
83GrDawnGpuTextureCommandBuffer::~GrDawnGpuTextureCommandBuffer() {}
84
85////////////////////////////////////////////////////////////////////////////////
86
Stephen White170d9902019-08-15 16:48:24 -040087static dawn::LoadOp to_dawn_load_op(GrLoadOp loadOp) {
Stephen White985741a2019-07-18 11:43:45 -040088 switch (loadOp) {
89 case GrLoadOp::kLoad:
90 return dawn::LoadOp::Load;
Stephen White170d9902019-08-15 16:48:24 -040091 case GrLoadOp::kDiscard:
92 // Use LoadOp::Load to emulate DontCare.
93 // Dawn doesn't have DontCare, for security reasons.
94 // Load should be equivalent to DontCare for desktop; Clear would
95 // probably be better for tilers. If Dawn does add DontCare
96 // as an extension, use it here.
97 return dawn::LoadOp::Load;
Stephen White985741a2019-07-18 11:43:45 -040098 case GrLoadOp::kClear:
99 return dawn::LoadOp::Clear;
Stephen White985741a2019-07-18 11:43:45 -0400100 default:
101 SK_ABORT("Invalid LoadOp");
Stephen White985741a2019-07-18 11:43:45 -0400102 }
103}
104
105GrDawnGpuRTCommandBuffer::GrDawnGpuRTCommandBuffer(GrDawnGpu* gpu,
106 GrRenderTarget* rt, GrSurfaceOrigin origin,
107 const LoadAndStoreInfo& colorInfo,
108 const StencilLoadAndStoreInfo& stencilInfo)
109 : INHERITED(rt, origin)
Stephen Whited67c71f2019-08-06 11:15:41 -0400110 , fGpu(gpu)
111 , fColorInfo(colorInfo) {
112 fEncoder = fGpu->device().CreateCommandEncoder();
Stephen Whitef813ef72019-08-09 12:28:37 -0400113 dawn::LoadOp colorOp = to_dawn_load_op(colorInfo.fLoadOp);
114 dawn::LoadOp stencilOp = to_dawn_load_op(stencilInfo.fLoadOp);
115 fPassEncoder = beginRenderPass(colorOp, stencilOp);
Stephen White985741a2019-07-18 11:43:45 -0400116}
117
Stephen Whitef813ef72019-08-09 12:28:37 -0400118dawn::RenderPassEncoder GrDawnGpuRTCommandBuffer::beginRenderPass(dawn::LoadOp colorOp,
119 dawn::LoadOp stencilOp) {
Stephen Whited67c71f2019-08-06 11:15:41 -0400120 dawn::Texture texture = static_cast<GrDawnRenderTarget*>(fRenderTarget)->texture();
Stephen Whitef813ef72019-08-09 12:28:37 -0400121 auto stencilAttachment = static_cast<GrDawnStencilAttachment*>(
122 fRenderTarget->renderTargetPriv().getStencilAttachment());
Stephen Whited67c71f2019-08-06 11:15:41 -0400123 dawn::TextureView colorView = texture.CreateDefaultView();
124 const float *c = fColorInfo.fClearColor.vec();
Stephen White985741a2019-07-18 11:43:45 -0400125
Stephen Whited67c71f2019-08-06 11:15:41 -0400126 dawn::RenderPassColorAttachmentDescriptor colorAttachment;
127 colorAttachment.attachment = colorView;
128 colorAttachment.resolveTarget = nullptr;
129 colorAttachment.clearColor = { c[0], c[1], c[2], c[3] };
130 colorAttachment.loadOp = colorOp;
131 colorAttachment.storeOp = dawn::StoreOp::Store;
132 dawn::RenderPassColorAttachmentDescriptor* colorAttachments = { &colorAttachment };
133 dawn::RenderPassDescriptor renderPassDescriptor;
134 renderPassDescriptor.colorAttachmentCount = 1;
135 renderPassDescriptor.colorAttachments = &colorAttachments;
Stephen Whitef813ef72019-08-09 12:28:37 -0400136 if (stencilAttachment) {
137 dawn::RenderPassDepthStencilAttachmentDescriptor depthStencilAttachment;
138 depthStencilAttachment.attachment = stencilAttachment->view();
139 depthStencilAttachment.depthLoadOp = stencilOp;
140 depthStencilAttachment.stencilLoadOp = stencilOp;
141 depthStencilAttachment.clearDepth = 1.0f;
142 depthStencilAttachment.clearStencil = 0;
143 depthStencilAttachment.depthStoreOp = dawn::StoreOp::Store;
144 depthStencilAttachment.stencilStoreOp = dawn::StoreOp::Store;
145 renderPassDescriptor.depthStencilAttachment = &depthStencilAttachment;
146 } else {
147 renderPassDescriptor.depthStencilAttachment = nullptr;
148 }
Stephen Whited67c71f2019-08-06 11:15:41 -0400149 return fEncoder.BeginRenderPass(&renderPassDescriptor);
150}
Stephen White985741a2019-07-18 11:43:45 -0400151
152GrDawnGpuRTCommandBuffer::~GrDawnGpuRTCommandBuffer() {
153}
154
155GrGpu* GrDawnGpuRTCommandBuffer::gpu() { return fGpu; }
156
157void GrDawnGpuRTCommandBuffer::end() {
Stephen Whited67c71f2019-08-06 11:15:41 -0400158 fPassEncoder.EndPass();
Stephen White985741a2019-07-18 11:43:45 -0400159}
160
161void GrDawnGpuRTCommandBuffer::submit() {
Stephen Whited67c71f2019-08-06 11:15:41 -0400162 dawn::CommandBuffer commandBuffer = fEncoder.Finish();
163 if (commandBuffer) {
164 fGpu->queue().Submit(1, &commandBuffer);
Stephen White985741a2019-07-18 11:43:45 -0400165 }
166}
167
168void GrDawnGpuRTCommandBuffer::insertEventMarker(const char* msg) {
Stephen Whitec0a837f2019-07-30 11:38:22 -0400169 SkASSERT(!"unimplemented");
Stephen White985741a2019-07-18 11:43:45 -0400170}
171
Stephen Whited7325182019-08-02 17:22:59 -0400172void GrDawnGpuRTCommandBuffer::transferFrom(const SkIRect& srcRect, GrColorType surfaceColorType,
173 GrColorType bufferColorType,
Stephen White985741a2019-07-18 11:43:45 -0400174 GrGpuBuffer* transferBuffer, size_t offset) {
175 fGpu->transferPixelsFrom(fRenderTarget, srcRect.fLeft, srcRect.fTop, srcRect.width(),
Stephen Whited7325182019-08-02 17:22:59 -0400176 srcRect.height(), surfaceColorType, bufferColorType, transferBuffer,
177 offset);
Stephen White985741a2019-07-18 11:43:45 -0400178}
179
180void GrDawnGpuRTCommandBuffer::onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
Stephen Whitef813ef72019-08-09 12:28:37 -0400181 fPassEncoder.EndPass();
182 fPassEncoder = beginRenderPass(dawn::LoadOp::Load, dawn::LoadOp::Clear);
Stephen White985741a2019-07-18 11:43:45 -0400183}
184
185void GrDawnGpuRTCommandBuffer::onClear(const GrFixedClip& clip, const SkPMColor4f& color) {
Stephen Whitef813ef72019-08-09 12:28:37 -0400186 fPassEncoder.EndPass();
187 fPassEncoder = beginRenderPass(dawn::LoadOp::Clear, dawn::LoadOp::Load);
Stephen White985741a2019-07-18 11:43:45 -0400188}
189
190////////////////////////////////////////////////////////////////////////////////
191
192void GrDawnGpuRTCommandBuffer::inlineUpload(GrOpFlushState* state,
193 GrDeferredTextureUploadFn& upload) {
Stephen Whitec0a837f2019-07-30 11:38:22 -0400194 SkASSERT(!"unimplemented");
Stephen White985741a2019-07-18 11:43:45 -0400195}
196
197void GrDawnGpuRTCommandBuffer::copy(GrSurface* src, const SkIRect& srcRect,
198 const SkIPoint& dstPoint) {
Stephen Whitee3b13892019-08-14 15:48:02 -0400199 auto s = static_cast<GrDawnTexture*>(src->asTexture());
200 auto d = static_cast<GrDawnTexture*>(fRenderTarget->asTexture());
201
202 if (!s || !d) {
203 return;
204 }
205
206 dawn::Texture srcTex = s->texture();
207 dawn::Texture dstTex = d->texture();
208
209 uint32_t x = srcRect.x();
210 uint32_t y = srcRect.y();
211 uint32_t width = srcRect.width();
212 uint32_t height = srcRect.height();
213 int rowPitch = GrDawnRoundRowBytes(width * GrBytesPerPixel(src->config()));
214 int sizeInBytes = rowPitch * height;
215
216 dawn::BufferDescriptor desc;
217 desc.usage = dawn::BufferUsageBit::CopySrc | dawn::BufferUsageBit::CopyDst;
218 desc.size = sizeInBytes;
219
220 dawn::Buffer buffer = fGpu->device().CreateBuffer(&desc);
221
222 uint32_t dstX = dstPoint.x();
223 uint32_t dstY = dstPoint.y();
224 fPassEncoder.EndPass();
225
226 dawn::TextureCopyView srcTextureCopyView;
227 srcTextureCopyView.texture = srcTex;
228 srcTextureCopyView.origin = {x, y, 0};
229
230 dawn::TextureCopyView dstTextureCopyView;
231 dstTextureCopyView.texture = dstTex;
232 dstTextureCopyView.origin = {dstX, dstY, 0};
233
234 dawn::BufferCopyView bufferCopyView;
235 bufferCopyView.buffer = buffer;
236 bufferCopyView.offset = 0;
237 bufferCopyView.rowPitch = rowPitch;
238 bufferCopyView.imageHeight = height;
239
240 dawn::Extent3D copySize = {width, height, 1};
241 fEncoder.CopyTextureToBuffer(&srcTextureCopyView, &bufferCopyView, &copySize);
242 fEncoder.CopyBufferToTexture(&bufferCopyView, &dstTextureCopyView, &copySize);
243 fPassEncoder = beginRenderPass(dawn::LoadOp::Load, dawn::LoadOp::Load);
Stephen White985741a2019-07-18 11:43:45 -0400244}
245
246////////////////////////////////////////////////////////////////////////////////
247
Stephen Whited67c71f2019-08-06 11:15:41 -0400248static dawn::VertexFormat to_dawn_vertex_format(GrVertexAttribType type) {
249 switch (type) {
250 case kFloat_GrVertexAttribType:
251 case kHalf_GrVertexAttribType:
252 return dawn::VertexFormat::Float;
253 case kFloat2_GrVertexAttribType:
254 case kHalf2_GrVertexAttribType:
255 return dawn::VertexFormat::Float2;
256 case kFloat3_GrVertexAttribType:
257 case kHalf3_GrVertexAttribType:
258 return dawn::VertexFormat::Float3;
259 case kFloat4_GrVertexAttribType:
260 case kHalf4_GrVertexAttribType:
261 return dawn::VertexFormat::Float4;
262 case kUShort2_GrVertexAttribType:
263 return dawn::VertexFormat::UShort2;
Stephen White170d9902019-08-15 16:48:24 -0400264 case kInt_GrVertexAttribType:
265 return dawn::VertexFormat::Int;
Stephen Whited67c71f2019-08-06 11:15:41 -0400266 case kUByte4_norm_GrVertexAttribType:
267 return dawn::VertexFormat::UChar4Norm;
268 default:
269 SkASSERT(!"unsupported vertex format");
270 return dawn::VertexFormat::Float4;
271 }
272}
273
Stephen Whitef813ef72019-08-09 12:28:37 -0400274static dawn::PrimitiveTopology to_dawn_primitive_topology(GrPrimitiveType primitiveType) {
275 switch (primitiveType) {
276 case GrPrimitiveType::kTriangles:
277 return dawn::PrimitiveTopology::TriangleList;
278 case GrPrimitiveType::kTriangleStrip:
279 return dawn::PrimitiveTopology::TriangleStrip;
280 case GrPrimitiveType::kPoints:
281 return dawn::PrimitiveTopology::PointList;
282 case GrPrimitiveType::kLines:
283 return dawn::PrimitiveTopology::LineList;
284 case GrPrimitiveType::kLineStrip:
285 return dawn::PrimitiveTopology::LineStrip;
286 case GrPrimitiveType::kLinesAdjacency:
287 default:
288 SkASSERT(!"unsupported primitive topology");
289 return dawn::PrimitiveTopology::TriangleList;
290 }
291}
292
Stephen White170d9902019-08-15 16:48:24 -0400293void GrDawnGpuRTCommandBuffer::setScissorState(
294 const GrPipeline& pipeline,
295 const GrPipeline::FixedDynamicState* fixedDynamicState,
296 const GrPipeline::DynamicStateArrays* dynamicStateArrays) {
297 SkIRect rect;
298 if (pipeline.isScissorEnabled()) {
299 constexpr SkIRect kBogusScissor{0, 0, 1, 1};
300 rect = fixedDynamicState ? fixedDynamicState->fScissorRect : kBogusScissor;
301 if (kBottomLeft_GrSurfaceOrigin == fOrigin) {
302 rect.setXYWH(rect.x(), fRenderTarget->height() - rect.bottom(),
303 rect.width(), rect.height());
304 }
305 } else {
306 rect = SkIRect::MakeWH(fRenderTarget->width(), fRenderTarget->height());
307 }
308 fPassEncoder.SetScissorRect(rect.x(), rect.y(), rect.width(), rect.height());
309}
310
Stephen Whitef813ef72019-08-09 12:28:37 -0400311void GrDawnGpuRTCommandBuffer::applyState(const GrPipeline& pipeline,
312 const GrPrimitiveProcessor& primProc,
313 const GrTextureProxy* const primProcProxies[],
314 const GrPipeline::FixedDynamicState* fixedDynamicState,
315 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
316 const GrPrimitiveType primitiveType,
317 bool hasPoints) {
Stephen Whited67c71f2019-08-06 11:15:41 -0400318 GrProgramDesc desc;
319 GrProgramDesc::Build(&desc, fRenderTarget, primProc, hasPoints, pipeline, fGpu);
320 dawn::TextureFormat colorFormat;
321 SkAssertResult(GrPixelConfigToDawnFormat(fRenderTarget->config(), &colorFormat));
Stephen Whitef813ef72019-08-09 12:28:37 -0400322 dawn::TextureFormat stencilFormat = dawn::TextureFormat::Depth24PlusStencil8;
323 bool hasDepthStencil = fRenderTarget->renderTargetPriv().getStencilAttachment() != nullptr;
Stephen Whited67c71f2019-08-06 11:15:41 -0400324 sk_sp<GrDawnProgram> program = GrDawnProgramBuilder::Build(fGpu, fRenderTarget, fOrigin,
325 pipeline, primProc, primProcProxies,
Stephen Whitef813ef72019-08-09 12:28:37 -0400326 colorFormat, hasDepthStencil,
327 stencilFormat, &desc);
Stephen Whited67c71f2019-08-06 11:15:41 -0400328 SkASSERT(program);
329 program->setData(primProc, fRenderTarget, fOrigin, pipeline);
330
331 std::vector<dawn::VertexBufferDescriptor> inputs;
332 std::vector<dawn::VertexAttributeDescriptor> vertexAttributes;
333 if (primProc.numVertexAttributes() > 0) {
334 size_t offset = 0;
335 int i = 0;
336 for (const auto& attrib : primProc.vertexAttributes()) {
337 dawn::VertexAttributeDescriptor attribute;
338 attribute.shaderLocation = i;
339 attribute.offset = offset;
340 attribute.format = to_dawn_vertex_format(attrib.cpuType());
341 vertexAttributes.push_back(attribute);
342 offset += attrib.sizeAlign4();
343 i++;
344 }
345 dawn::VertexBufferDescriptor input;
346 input.stride = offset;
347 input.stepMode = dawn::InputStepMode::Vertex;
348 input.attributes = &vertexAttributes.front();
349 input.attributeCount = vertexAttributes.size();
350 inputs.push_back(input);
351 }
352 std::vector<dawn::VertexAttributeDescriptor> instanceAttributes;
353 if (primProc.numInstanceAttributes() > 0) {
354 size_t offset = 0;
355 int i = 0;
356 for (const auto& attrib : primProc.instanceAttributes()) {
357 dawn::VertexAttributeDescriptor attribute;
358 attribute.shaderLocation = i;
359 attribute.offset = offset;
360 attribute.format = to_dawn_vertex_format(attrib.cpuType());
361 instanceAttributes.push_back(attribute);
362 offset += attrib.sizeAlign4();
363 i++;
364 }
365 dawn::VertexBufferDescriptor input;
366 input.stride = offset;
367 input.stepMode = dawn::InputStepMode::Instance;
368 input.attributes = &instanceAttributes.front();
369 input.attributeCount = instanceAttributes.size();
370 inputs.push_back(input);
371 }
372 dawn::VertexInputDescriptor vertexInput;
373 vertexInput.bufferCount = inputs.size();
374 vertexInput.buffers = &inputs.front();
375 vertexInput.indexFormat = dawn::IndexFormat::Uint16;
376
377 dawn::PipelineStageDescriptor vsDesc;
378 vsDesc.module = program->fVSModule;
379 vsDesc.entryPoint = "main";
380
381 dawn::PipelineStageDescriptor fsDesc;
382 fsDesc.module = program->fFSModule;
383 fsDesc.entryPoint = "main";
384
Stephen Whited67c71f2019-08-06 11:15:41 -0400385 dawn::RasterizationStateDescriptor rastDesc;
386
387 rastDesc.frontFace = dawn::FrontFace::CW;
388 rastDesc.cullMode = dawn::CullMode::None;
389 rastDesc.depthBias = 0;
390 rastDesc.depthBiasSlopeScale = 0.0f;
391 rastDesc.depthBiasClamp = 0.0f;
392
393 dawn::RenderPipelineDescriptor rpDesc;
394 rpDesc.layout = program->fPipelineLayout;
395 rpDesc.vertexStage = &vsDesc;
396 rpDesc.fragmentStage = &fsDesc;
397 rpDesc.vertexInput = &vertexInput;
398 rpDesc.rasterizationState = &rastDesc;
Stephen Whitef813ef72019-08-09 12:28:37 -0400399 rpDesc.primitiveTopology = to_dawn_primitive_topology(primitiveType);
Stephen Whited67c71f2019-08-06 11:15:41 -0400400 rpDesc.sampleCount = 1;
Stephen Whitef813ef72019-08-09 12:28:37 -0400401 rpDesc.depthStencilState = hasDepthStencil ? &program->fDepthStencilState : nullptr;
Stephen Whited67c71f2019-08-06 11:15:41 -0400402 rpDesc.colorStateCount = 1;
403 dawn::ColorStateDescriptor* colorStates[] = { &program->fColorState };
404 rpDesc.colorStates = colorStates;
405 dawn::RenderPipeline renderPipeline = fGpu->device().CreateRenderPipeline(&rpDesc);
406 fPassEncoder.SetPipeline(renderPipeline);
Stephen White170d9902019-08-15 16:48:24 -0400407 fPassEncoder.SetBindGroup(0, program->fBindGroup, 0, nullptr);
Stephen Whitef813ef72019-08-09 12:28:37 -0400408 if (pipeline.isStencilEnabled()) {
409 fPassEncoder.SetStencilReference(pipeline.getUserStencil()->fFront.fRef);
410 }
Stephen White170d9902019-08-15 16:48:24 -0400411 GrXferProcessor::BlendInfo blendInfo = pipeline.getXferProcessor().getBlendInfo();
412 const float* c = blendInfo.fBlendConstant.vec();
413 dawn::Color color{c[0], c[1], c[2], c[3]};
414 fPassEncoder.SetBlendColor(&color);
415 this->setScissorState(pipeline, fixedDynamicState, dynamicStateArrays);
Stephen White985741a2019-07-18 11:43:45 -0400416}
417
418void GrDawnGpuRTCommandBuffer::onDraw(const GrPrimitiveProcessor& primProc,
419 const GrPipeline& pipeline,
420 const GrPipeline::FixedDynamicState* fixedDynamicState,
421 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
422 const GrMesh meshes[],
423 int meshCount,
424 const SkRect& bounds) {
425 if (!meshCount) {
426 return;
427 }
Stephen Whited67c71f2019-08-06 11:15:41 -0400428 bool hasPoints = false;
Stephen White985741a2019-07-18 11:43:45 -0400429 for (int i = 0; i < meshCount; ++i) {
Stephen Whited67c71f2019-08-06 11:15:41 -0400430 if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) {
431 hasPoints = true;
432 }
Stephen White985741a2019-07-18 11:43:45 -0400433 }
Stephen Whited67c71f2019-08-06 11:15:41 -0400434 const GrTextureProxy* const* primProcProxies = nullptr;
435 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
436 primProcProxies = dynamicStateArrays->fPrimitiveProcessorTextures;
437 } else if (fixedDynamicState) {
438 primProcProxies = fixedDynamicState->fPrimitiveProcessorTextures;
439 }
Stephen Whited67c71f2019-08-06 11:15:41 -0400440 for (int i = 0; i < meshCount; ++i) {
Stephen Whitef813ef72019-08-09 12:28:37 -0400441 applyState(pipeline, primProc, primProcProxies, fixedDynamicState, dynamicStateArrays,
442 meshes[0].primitiveType(), hasPoints);
Stephen Whited67c71f2019-08-06 11:15:41 -0400443 meshes[i].sendToGpu(this);
444 }
Stephen White985741a2019-07-18 11:43:45 -0400445}
446
447void GrDawnGpuRTCommandBuffer::sendInstancedMeshToGpu(GrPrimitiveType,
448 const GrBuffer* vertexBuffer,
449 int vertexCount,
450 int baseVertex,
451 const GrBuffer* instanceBuffer,
452 int instanceCount,
453 int baseInstance) {
Stephen Whited67c71f2019-08-06 11:15:41 -0400454 static const uint64_t vertexBufferOffsets[1] = {0};
455 dawn::Buffer vb = static_cast<const GrDawnBuffer*>(vertexBuffer)->get();
456 fPassEncoder.SetVertexBuffers(0, 1, &vb, vertexBufferOffsets);
457 fPassEncoder.Draw(vertexCount, 1, baseVertex, baseInstance);
Stephen White985741a2019-07-18 11:43:45 -0400458 fGpu->stats()->incNumDraws();
459}
460
461void GrDawnGpuRTCommandBuffer::sendIndexedInstancedMeshToGpu(GrPrimitiveType,
462 const GrBuffer* indexBuffer,
463 int indexCount,
464 int baseIndex,
465 const GrBuffer* vertexBuffer,
466 int baseVertex,
467 const GrBuffer* instanceBuffer,
468 int instanceCount,
469 int baseInstance,
470 GrPrimitiveRestart restart) {
Stephen Whited67c71f2019-08-06 11:15:41 -0400471 uint64_t vertexBufferOffsets[1];
472 vertexBufferOffsets[0] = 0;
473 dawn::Buffer vb = static_cast<const GrDawnBuffer*>(vertexBuffer)->get();
474 dawn::Buffer ib = static_cast<const GrDawnBuffer*>(indexBuffer)->get();
475 fPassEncoder.SetIndexBuffer(ib, 0);
476 fPassEncoder.SetVertexBuffers(0, 1, &vb, vertexBufferOffsets);
477 fPassEncoder.DrawIndexed(indexCount, 1, baseIndex, baseVertex, baseInstance);
Stephen White985741a2019-07-18 11:43:45 -0400478 fGpu->stats()->incNumDraws();
479}