blob: 3aae8c9ce8bb9889eca4e80cb6cd0a5ac1c63dbb [file] [log] [blame]
Corentin Walleze1f0d4e2019-02-15 12:54:08 +00001// Copyright 2019 The Dawn Authors
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#include "dawn_native/CommandEncoder.h"
16
Corentin Wallezf20f5b92019-02-20 11:46:16 +000017#include "common/BitSetIterator.h"
18#include "dawn_native/BindGroup.h"
19#include "dawn_native/Buffer.h"
Corentin Walleze1f0d4e2019-02-15 12:54:08 +000020#include "dawn_native/CommandBuffer.h"
Corentin Wallezf20f5b92019-02-20 11:46:16 +000021#include "dawn_native/CommandBufferStateTracker.h"
22#include "dawn_native/Commands.h"
23#include "dawn_native/ComputePassEncoder.h"
Corentin Walleze1f0d4e2019-02-15 12:54:08 +000024#include "dawn_native/Device.h"
Corentin Wallezf20f5b92019-02-20 11:46:16 +000025#include "dawn_native/ErrorData.h"
Corentin Wallezf20f5b92019-02-20 11:46:16 +000026#include "dawn_native/RenderPassEncoder.h"
27#include "dawn_native/RenderPipeline.h"
28
29#include <map>
Corentin Walleze1f0d4e2019-02-15 12:54:08 +000030
31namespace dawn_native {
32
Corentin Wallezf20f5b92019-02-20 11:46:16 +000033 namespace {
34
35 MaybeError ValidateCopySizeFitsInTexture(const TextureCopy& textureCopy,
36 const Extent3D& copySize) {
37 const TextureBase* texture = textureCopy.texture.Get();
38 if (textureCopy.level >= texture->GetNumMipLevels()) {
39 return DAWN_VALIDATION_ERROR("Copy mip-level out of range");
40 }
41
42 if (textureCopy.slice >= texture->GetArrayLayers()) {
43 return DAWN_VALIDATION_ERROR("Copy array-layer out of range");
44 }
45
46 // All texture dimensions are in uint32_t so by doing checks in uint64_t we avoid
47 // overflows.
48 uint64_t level = textureCopy.level;
49 if (uint64_t(textureCopy.origin.x) + uint64_t(copySize.width) >
50 (static_cast<uint64_t>(texture->GetSize().width) >> level) ||
51 uint64_t(textureCopy.origin.y) + uint64_t(copySize.height) >
52 (static_cast<uint64_t>(texture->GetSize().height) >> level)) {
53 return DAWN_VALIDATION_ERROR("Copy would touch outside of the texture");
54 }
55
56 // TODO(cwallez@chromium.org): Check the depth bound differently for 2D arrays and 3D
57 // textures
58 if (textureCopy.origin.z != 0 || copySize.depth != 1) {
59 return DAWN_VALIDATION_ERROR("No support for z != 0 and depth != 1 for now");
60 }
61
62 return {};
63 }
64
65 bool FitsInBuffer(const BufferBase* buffer, uint32_t offset, uint32_t size) {
66 uint32_t bufferSize = buffer->GetSize();
67 return offset <= bufferSize && (size <= (bufferSize - offset));
68 }
69
70 MaybeError ValidateCopySizeFitsInBuffer(const BufferCopy& bufferCopy, uint32_t dataSize) {
71 if (!FitsInBuffer(bufferCopy.buffer.Get(), bufferCopy.offset, dataSize)) {
72 return DAWN_VALIDATION_ERROR("Copy would overflow the buffer");
73 }
74
75 return {};
76 }
77
Yan, Shaobo738567f2019-02-27 02:46:27 +000078 MaybeError ValidateB2BCopySizeAlignment(uint32_t dataSize,
79 uint32_t srcOffset,
80 uint32_t dstOffset) {
81 // Copy size must be a multiple of 4 bytes on macOS.
82 if (dataSize % 4 != 0) {
83 return DAWN_VALIDATION_ERROR("Copy size must be a multiple of 4 bytes");
84 }
85
86 // SourceOffset and destinationOffset must be multiples of 4 bytes on macOS.
87 if (srcOffset % 4 != 0 || dstOffset % 4 != 0) {
88 return DAWN_VALIDATION_ERROR(
89 "Source offset and destination offset must be multiples of 4 bytes");
90 }
91
92 return {};
93 }
94
Corentin Wallezf20f5b92019-02-20 11:46:16 +000095 MaybeError ValidateTexelBufferOffset(TextureBase* texture, const BufferCopy& bufferCopy) {
96 uint32_t texelSize =
97 static_cast<uint32_t>(TextureFormatPixelSize(texture->GetFormat()));
98 if (bufferCopy.offset % texelSize != 0) {
99 return DAWN_VALIDATION_ERROR("Buffer offset must be a multiple of the texel size");
100 }
101
102 return {};
103 }
104
105 MaybeError ValidateImageHeight(uint32_t imageHeight, uint32_t copyHeight) {
106 if (imageHeight < copyHeight) {
107 return DAWN_VALIDATION_ERROR("Image height must not be less than the copy height.");
108 }
109
110 return {};
111 }
112
Brandon Jones11d32c82019-02-20 20:21:00 +0000113 inline MaybeError PushDebugMarkerStack(unsigned int* counter) {
114 *counter += 1;
115 return {};
116 }
117
118 inline MaybeError PopDebugMarkerStack(unsigned int* counter) {
119 if (*counter == 0) {
120 return DAWN_VALIDATION_ERROR("Pop must be balanced by a corresponding Push.");
121 } else {
122 *counter -= 1;
123 }
124
125 return {};
126 }
127
128 inline MaybeError ValidateDebugGroups(const unsigned int counter) {
129 if (counter != 0) {
130 return DAWN_VALIDATION_ERROR("Each Push must be balanced by a corresponding Pop.");
131 }
132
133 return {};
134 }
135
Jiawei Shao081d5c22019-03-04 12:01:59 +0000136 MaybeError ValidateTextureSampleCountInCopyCommands(const TextureBase* texture) {
137 if (texture->GetSampleCount() > 1) {
138 return DAWN_VALIDATION_ERROR("The sample count of textures must be 1");
139 }
140
141 return {};
142 }
143
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000144 MaybeError ComputeTextureCopyBufferSize(const Extent3D& copySize,
145 uint32_t rowPitch,
146 uint32_t imageHeight,
147 uint32_t* bufferSize) {
148 DAWN_TRY(ValidateImageHeight(imageHeight, copySize.height));
149
150 // TODO(cwallez@chromium.org): check for overflows
151 uint32_t slicePitch = rowPitch * imageHeight;
152 uint32_t sliceSize = rowPitch * (copySize.height - 1) + copySize.width;
153 *bufferSize = (slicePitch * (copySize.depth - 1)) + sliceSize;
154
155 return {};
156 }
157
158 uint32_t ComputeDefaultRowPitch(TextureBase* texture, uint32_t width) {
159 uint32_t texelSize = TextureFormatPixelSize(texture->GetFormat());
160 return texelSize * width;
161 }
162
163 MaybeError ValidateRowPitch(dawn::TextureFormat format,
164 const Extent3D& copySize,
165 uint32_t rowPitch) {
166 if (rowPitch % kTextureRowPitchAlignment != 0) {
167 return DAWN_VALIDATION_ERROR("Row pitch must be a multiple of 256");
168 }
169
170 uint32_t texelSize = TextureFormatPixelSize(format);
171 if (rowPitch < copySize.width * texelSize) {
172 return DAWN_VALIDATION_ERROR(
173 "Row pitch must not be less than the number of bytes per row");
174 }
175
176 return {};
177 }
178
179 MaybeError ValidateCanUseAs(BufferBase* buffer, dawn::BufferUsageBit usage) {
180 ASSERT(HasZeroOrOneBits(usage));
181 if (!(buffer->GetUsage() & usage)) {
182 return DAWN_VALIDATION_ERROR("buffer doesn't have the required usage.");
183 }
184
185 return {};
186 }
187
188 MaybeError ValidateCanUseAs(TextureBase* texture, dawn::TextureUsageBit usage) {
189 ASSERT(HasZeroOrOneBits(usage));
190 if (!(texture->GetUsage() & usage)) {
191 return DAWN_VALIDATION_ERROR("texture doesn't have the required usage.");
192 }
193
194 return {};
195 }
196
Jiawei Shaob2c50232019-02-27 09:21:56 +0000197 MaybeError ValidateAttachmentArrayLayersAndLevelCount(const TextureViewBase* attachment) {
198 // Currently we do not support layered rendering.
199 if (attachment->GetLayerCount() > 1) {
200 return DAWN_VALIDATION_ERROR(
201 "The layer count of the texture view used as attachment cannot be greater than "
202 "1");
203 }
204
205 if (attachment->GetLevelCount() > 1) {
206 return DAWN_VALIDATION_ERROR(
207 "The mipmap level count of the texture view used as attachment cannot be "
208 "greater than 1");
209 }
210
211 return {};
212 }
213
214 MaybeError ValidateOrSetAttachmentSize(const TextureViewBase* attachment,
215 uint32_t* width,
216 uint32_t* height) {
217 const Extent3D& textureSize = attachment->GetTexture()->GetSize();
218 const uint32_t attachmentWidth = textureSize.width >> attachment->GetBaseMipLevel();
219 const uint32_t attachmentHeight = textureSize.height >> attachment->GetBaseMipLevel();
220
221 if (*width == 0) {
222 DAWN_ASSERT(*height == 0);
223 *width = attachmentWidth;
224 *height = attachmentHeight;
225 DAWN_ASSERT(*width != 0 && *height != 0);
226 } else if (*width != attachmentWidth || *height != attachmentHeight) {
227 return DAWN_VALIDATION_ERROR("Attachment size mismatch");
228 }
229
230 return {};
231 }
232
Jiawei Shao1e1c13e2019-03-11 18:41:02 +0000233 MaybeError ValidateOrSetColorAttachmentSampleCount(const TextureViewBase* colorAttachment,
234 uint32_t* sampleCount) {
235 if (*sampleCount == 0) {
236 *sampleCount = colorAttachment->GetTexture()->GetSampleCount();
237 DAWN_ASSERT(*sampleCount != 0);
238 } else if (*sampleCount != colorAttachment->GetTexture()->GetSampleCount()) {
239 return DAWN_VALIDATION_ERROR("Color attachment sample counts mismatch");
240 }
241
242 return {};
243 }
244
245 MaybeError ValidateResolveTarget(
246 const DeviceBase* device,
247 const RenderPassColorAttachmentDescriptor* colorAttachment) {
248 if (colorAttachment->resolveTarget == nullptr) {
249 return {};
250 }
251
252 DAWN_TRY(device->ValidateObject(colorAttachment->resolveTarget));
253
254 if (!colorAttachment->attachment->GetTexture()->IsMultisampledTexture()) {
255 return DAWN_VALIDATION_ERROR(
256 "Cannot set resolve target when the sample count of the color attachment is 1");
257 }
258
259 if (colorAttachment->resolveTarget->GetTexture()->IsMultisampledTexture()) {
260 return DAWN_VALIDATION_ERROR("Cannot use multisampled texture as resolve target");
261 }
262
263 if (colorAttachment->resolveTarget->GetLayerCount() > 1) {
264 return DAWN_VALIDATION_ERROR(
265 "The array layer count of the resolve target must be 1");
266 }
267
268 if (colorAttachment->resolveTarget->GetLevelCount() > 1) {
269 return DAWN_VALIDATION_ERROR("The mip level count of the resolve target must be 1");
270 }
271
272 uint32_t colorAttachmentBaseMipLevel = colorAttachment->attachment->GetBaseMipLevel();
273 const Extent3D& colorTextureSize = colorAttachment->attachment->GetTexture()->GetSize();
274 uint32_t colorAttachmentWidth = colorTextureSize.width >> colorAttachmentBaseMipLevel;
275 uint32_t colorAttachmentHeight = colorTextureSize.height >> colorAttachmentBaseMipLevel;
276
277 uint32_t resolveTargetBaseMipLevel = colorAttachment->resolveTarget->GetBaseMipLevel();
278 const Extent3D& resolveTextureSize =
279 colorAttachment->resolveTarget->GetTexture()->GetSize();
280 uint32_t resolveTargetWidth = resolveTextureSize.width >> resolveTargetBaseMipLevel;
281 uint32_t resolveTargetHeight = resolveTextureSize.height >> resolveTargetBaseMipLevel;
282 if (colorAttachmentWidth != resolveTargetWidth ||
283 colorAttachmentHeight != resolveTargetHeight) {
284 return DAWN_VALIDATION_ERROR(
285 "The size of the resolve target must be the same as the color attachment");
286 }
287
288 dawn::TextureFormat resolveTargetFormat = colorAttachment->resolveTarget->GetFormat();
289 if (resolveTargetFormat != colorAttachment->attachment->GetFormat()) {
290 return DAWN_VALIDATION_ERROR(
291 "The format of the resolve target must be the same as the color attachment");
292 }
293
294 return {};
295 }
296
Jiawei Shaob2c50232019-02-27 09:21:56 +0000297 MaybeError ValidateRenderPassColorAttachment(
298 const DeviceBase* device,
299 const RenderPassColorAttachmentDescriptor* colorAttachment,
300 uint32_t* width,
Jiawei Shao1e1c13e2019-03-11 18:41:02 +0000301 uint32_t* height,
302 uint32_t* sampleCount) {
Jiawei Shaob2c50232019-02-27 09:21:56 +0000303 DAWN_ASSERT(colorAttachment != nullptr);
304
305 DAWN_TRY(device->ValidateObject(colorAttachment->attachment));
306
Jiawei Shaob2c50232019-02-27 09:21:56 +0000307 const TextureViewBase* attachment = colorAttachment->attachment;
308 if (!IsColorRenderableTextureFormat(attachment->GetFormat())) {
309 return DAWN_VALIDATION_ERROR(
310 "The format of the texture view used as color attachment is not color "
311 "renderable");
312 }
313
Jiawei Shao1e1c13e2019-03-11 18:41:02 +0000314 DAWN_TRY(ValidateOrSetColorAttachmentSampleCount(attachment, sampleCount));
315
316 DAWN_TRY(ValidateResolveTarget(device, colorAttachment));
317
Jiawei Shaob2c50232019-02-27 09:21:56 +0000318 DAWN_TRY(ValidateAttachmentArrayLayersAndLevelCount(attachment));
319 DAWN_TRY(ValidateOrSetAttachmentSize(attachment, width, height));
320
321 return {};
322 }
323
324 MaybeError ValidateRenderPassDepthStencilAttachment(
325 const DeviceBase* device,
326 const RenderPassDepthStencilAttachmentDescriptor* depthStencilAttachment,
327 uint32_t* width,
Jiawei Shao93131172019-03-15 05:16:41 +0000328 uint32_t* height,
329 uint32_t* sampleCount) {
Jiawei Shaob2c50232019-02-27 09:21:56 +0000330 DAWN_ASSERT(depthStencilAttachment != nullptr);
331
332 DAWN_TRY(device->ValidateObject(depthStencilAttachment->attachment));
333
334 const TextureViewBase* attachment = depthStencilAttachment->attachment;
335 if (!TextureFormatHasDepthOrStencil(attachment->GetFormat())) {
336 return DAWN_VALIDATION_ERROR(
337 "The format of the texture view used as depth stencil attachment is not a "
338 "depth stencil format");
339 }
340
Jiawei Shao93131172019-03-15 05:16:41 +0000341 // *sampleCount == 0 must only happen when there is no color attachment. In that case we
342 // do not need to validate the sample count of the depth stencil attachment.
343 if (*sampleCount != 0 && (attachment->GetTexture()->GetSampleCount() != *sampleCount)) {
344 return DAWN_VALIDATION_ERROR("Depth stencil attachment sample counts mismatch");
345 }
346
Jiawei Shaob2c50232019-02-27 09:21:56 +0000347 DAWN_TRY(ValidateAttachmentArrayLayersAndLevelCount(attachment));
348 DAWN_TRY(ValidateOrSetAttachmentSize(attachment, width, height));
349
350 return {};
351 }
352
353 MaybeError ValidateRenderPassDescriptorAndSetSize(const DeviceBase* device,
354 const RenderPassDescriptor* renderPass,
355 uint32_t* width,
356 uint32_t* height) {
357 if (renderPass->colorAttachmentCount > kMaxColorAttachments) {
358 return DAWN_VALIDATION_ERROR("Setting color attachments out of bounds");
359 }
360
Jiawei Shao1e1c13e2019-03-11 18:41:02 +0000361 uint32_t sampleCount = 0;
Jiawei Shaob2c50232019-02-27 09:21:56 +0000362 for (uint32_t i = 0; i < renderPass->colorAttachmentCount; ++i) {
363 DAWN_TRY(ValidateRenderPassColorAttachment(device, renderPass->colorAttachments[i],
Jiawei Shao1e1c13e2019-03-11 18:41:02 +0000364 width, height, &sampleCount));
Jiawei Shaob2c50232019-02-27 09:21:56 +0000365 }
366
367 if (renderPass->depthStencilAttachment != nullptr) {
368 DAWN_TRY(ValidateRenderPassDepthStencilAttachment(
Jiawei Shao93131172019-03-15 05:16:41 +0000369 device, renderPass->depthStencilAttachment, width, height, &sampleCount));
Jiawei Shaob2c50232019-02-27 09:21:56 +0000370 }
371
372 if (renderPass->colorAttachmentCount == 0 &&
373 renderPass->depthStencilAttachment == nullptr) {
374 return DAWN_VALIDATION_ERROR("Cannot use render pass with no attachments.");
375 }
376
377 return {};
378 }
379
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000380 enum class PassType {
381 Render,
382 Compute,
383 };
384
385 // Helper class to encapsulate the logic of tracking per-resource usage during the
386 // validation of command buffer passes. It is used both to know if there are validation
387 // errors, and to get a list of resources used per pass for backends that need the
388 // information.
389 class PassResourceUsageTracker {
390 public:
391 void BufferUsedAs(BufferBase* buffer, dawn::BufferUsageBit usage) {
392 // std::map's operator[] will create the key and return 0 if the key didn't exist
393 // before.
394 dawn::BufferUsageBit& storedUsage = mBufferUsages[buffer];
395
396 if (usage == dawn::BufferUsageBit::Storage &&
397 storedUsage & dawn::BufferUsageBit::Storage) {
398 mStorageUsedMultipleTimes = true;
399 }
400
401 storedUsage |= usage;
402 }
403
404 void TextureUsedAs(TextureBase* texture, dawn::TextureUsageBit usage) {
405 // std::map's operator[] will create the key and return 0 if the key didn't exist
406 // before.
407 dawn::TextureUsageBit& storedUsage = mTextureUsages[texture];
408
409 if (usage == dawn::TextureUsageBit::Storage &&
410 storedUsage & dawn::TextureUsageBit::Storage) {
411 mStorageUsedMultipleTimes = true;
412 }
413
414 storedUsage |= usage;
415 }
416
417 // Performs the per-pass usage validation checks
418 MaybeError ValidateUsages(PassType pass) const {
419 // Storage resources cannot be used twice in the same compute pass
420 if (pass == PassType::Compute && mStorageUsedMultipleTimes) {
421 return DAWN_VALIDATION_ERROR(
422 "Storage resource used multiple times in compute pass");
423 }
424
425 // Buffers can only be used as single-write or multiple read.
426 for (auto& it : mBufferUsages) {
427 BufferBase* buffer = it.first;
428 dawn::BufferUsageBit usage = it.second;
429
430 if (usage & ~buffer->GetUsage()) {
431 return DAWN_VALIDATION_ERROR("Buffer missing usage for the pass");
432 }
433
434 bool readOnly = (usage & kReadOnlyBufferUsages) == usage;
435 bool singleUse = dawn::HasZeroOrOneBits(usage);
436
437 if (!readOnly && !singleUse) {
438 return DAWN_VALIDATION_ERROR(
439 "Buffer used as writeable usage and another usage in pass");
440 }
441 }
442
443 // Textures can only be used as single-write or multiple read.
444 // TODO(cwallez@chromium.org): implement per-subresource tracking
445 for (auto& it : mTextureUsages) {
446 TextureBase* texture = it.first;
447 dawn::TextureUsageBit usage = it.second;
448
449 if (usage & ~texture->GetUsage()) {
450 return DAWN_VALIDATION_ERROR("Texture missing usage for the pass");
451 }
452
453 // For textures the only read-only usage in a pass is Sampled, so checking the
454 // usage constraint simplifies to checking a single usage bit is set.
455 if (!dawn::HasZeroOrOneBits(it.second)) {
456 return DAWN_VALIDATION_ERROR(
457 "Texture used with more than one usage in pass");
458 }
459 }
460
461 return {};
462 }
463
464 // Returns the per-pass usage for use by backends for APIs with explicit barriers.
465 PassResourceUsage AcquireResourceUsage() {
466 PassResourceUsage result;
467 result.buffers.reserve(mBufferUsages.size());
468 result.bufferUsages.reserve(mBufferUsages.size());
469 result.textures.reserve(mTextureUsages.size());
470 result.textureUsages.reserve(mTextureUsages.size());
471
472 for (auto& it : mBufferUsages) {
473 result.buffers.push_back(it.first);
474 result.bufferUsages.push_back(it.second);
475 }
476
477 for (auto& it : mTextureUsages) {
478 result.textures.push_back(it.first);
479 result.textureUsages.push_back(it.second);
480 }
481
482 return result;
483 }
484
485 private:
486 std::map<BufferBase*, dawn::BufferUsageBit> mBufferUsages;
487 std::map<TextureBase*, dawn::TextureUsageBit> mTextureUsages;
488 bool mStorageUsedMultipleTimes = false;
489 };
490
491 void TrackBindGroupResourceUsage(BindGroupBase* group, PassResourceUsageTracker* tracker) {
492 const auto& layoutInfo = group->GetLayout()->GetBindingInfo();
493
494 for (uint32_t i : IterateBitSet(layoutInfo.mask)) {
495 dawn::BindingType type = layoutInfo.types[i];
496
497 switch (type) {
498 case dawn::BindingType::UniformBuffer: {
499 BufferBase* buffer = group->GetBindingAsBufferBinding(i).buffer;
500 tracker->BufferUsedAs(buffer, dawn::BufferUsageBit::Uniform);
501 } break;
502
503 case dawn::BindingType::StorageBuffer: {
504 BufferBase* buffer = group->GetBindingAsBufferBinding(i).buffer;
505 tracker->BufferUsedAs(buffer, dawn::BufferUsageBit::Storage);
506 } break;
507
508 case dawn::BindingType::SampledTexture: {
509 TextureBase* texture = group->GetBindingAsTextureView(i)->GetTexture();
510 tracker->TextureUsedAs(texture, dawn::TextureUsageBit::Sampled);
511 } break;
512
513 case dawn::BindingType::Sampler:
514 break;
515 }
516 }
517 }
518
519 } // namespace
520
521 enum class CommandEncoderBase::EncodingState : uint8_t {
522 TopLevel,
523 ComputePass,
524 RenderPass,
525 Finished
526 };
527
528 CommandEncoderBase::CommandEncoderBase(DeviceBase* device)
529 : ObjectBase(device), mEncodingState(EncodingState::TopLevel) {
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000530 }
531
532 CommandEncoderBase::~CommandEncoderBase() {
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000533 if (!mWereCommandsAcquired) {
534 MoveToIterator();
535 FreeCommands(&mIterator);
536 }
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000537 }
538
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000539 CommandIterator CommandEncoderBase::AcquireCommands() {
540 ASSERT(!mWereCommandsAcquired);
541 mWereCommandsAcquired = true;
542 return std::move(mIterator);
543 }
544
545 CommandBufferResourceUsage CommandEncoderBase::AcquireResourceUsages() {
546 ASSERT(!mWereResourceUsagesAcquired);
547 mWereResourceUsagesAcquired = true;
548 return std::move(mResourceUsages);
549 }
550
551 void CommandEncoderBase::MoveToIterator() {
552 if (!mWasMovedToIterator) {
553 mIterator = std::move(mAllocator);
554 mWasMovedToIterator = true;
555 }
556 }
557
558 // Implementation of the API's command recording methods
559
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000560 ComputePassEncoderBase* CommandEncoderBase::BeginComputePass() {
Jiawei Shaob47470d2019-03-05 01:02:47 +0000561 DeviceBase* device = GetDevice();
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000562 if (ConsumedError(ValidateCanRecordTopLevelCommands())) {
Jiawei Shaob47470d2019-03-05 01:02:47 +0000563 return ComputePassEncoderBase::MakeError(device, this);
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000564 }
565
566 mAllocator.Allocate<BeginComputePassCmd>(Command::BeginComputePass);
567
568 mEncodingState = EncodingState::ComputePass;
Jiawei Shaob47470d2019-03-05 01:02:47 +0000569 return new ComputePassEncoderBase(device, this, &mAllocator);
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000570 }
571
Jiawei Shaob2c50232019-02-27 09:21:56 +0000572 RenderPassEncoderBase* CommandEncoderBase::BeginRenderPass(const RenderPassDescriptor* info) {
573 DeviceBase* device = GetDevice();
574
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000575 if (ConsumedError(ValidateCanRecordTopLevelCommands())) {
Jiawei Shaob47470d2019-03-05 01:02:47 +0000576 return RenderPassEncoderBase::MakeError(device, this);
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000577 }
578
Jiawei Shaob2c50232019-02-27 09:21:56 +0000579 uint32_t width = 0;
580 uint32_t height = 0;
581 if (ConsumedError(ValidateRenderPassDescriptorAndSetSize(device, info, &width, &height))) {
Jiawei Shaob47470d2019-03-05 01:02:47 +0000582 return RenderPassEncoderBase::MakeError(device, this);
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000583 }
584
Jiawei Shaob2c50232019-02-27 09:21:56 +0000585 mEncodingState = EncodingState::RenderPass;
586
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000587 BeginRenderPassCmd* cmd = mAllocator.Allocate<BeginRenderPassCmd>(Command::BeginRenderPass);
588 new (cmd) BeginRenderPassCmd;
589
Jiawei Shaob2c50232019-02-27 09:21:56 +0000590 for (uint32_t i = 0; i < info->colorAttachmentCount; ++i) {
591 if (info->colorAttachments[i] != nullptr) {
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000592 cmd->colorAttachmentsSet.set(i);
Jiawei Shaob2c50232019-02-27 09:21:56 +0000593 cmd->colorAttachments[i].view = info->colorAttachments[i]->attachment;
594 cmd->colorAttachments[i].resolveTarget = info->colorAttachments[i]->resolveTarget;
595 cmd->colorAttachments[i].loadOp = info->colorAttachments[i]->loadOp;
596 cmd->colorAttachments[i].storeOp = info->colorAttachments[i]->storeOp;
597 cmd->colorAttachments[i].clearColor = info->colorAttachments[i]->clearColor;
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000598 }
599 }
600
Jiawei Shaob2c50232019-02-27 09:21:56 +0000601 cmd->hasDepthStencilAttachment = info->depthStencilAttachment != nullptr;
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000602 if (cmd->hasDepthStencilAttachment) {
Jiawei Shaob2c50232019-02-27 09:21:56 +0000603 cmd->hasDepthStencilAttachment = true;
604 cmd->depthStencilAttachment.view = info->depthStencilAttachment->attachment;
605 cmd->depthStencilAttachment.clearDepth = info->depthStencilAttachment->clearDepth;
606 cmd->depthStencilAttachment.clearStencil = info->depthStencilAttachment->clearStencil;
607 cmd->depthStencilAttachment.depthLoadOp = info->depthStencilAttachment->depthLoadOp;
608 cmd->depthStencilAttachment.depthStoreOp = info->depthStencilAttachment->depthStoreOp;
609 cmd->depthStencilAttachment.stencilLoadOp = info->depthStencilAttachment->stencilLoadOp;
610 cmd->depthStencilAttachment.stencilStoreOp =
611 info->depthStencilAttachment->stencilStoreOp;
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000612 }
613
Jiawei Shaob2c50232019-02-27 09:21:56 +0000614 cmd->width = width;
615 cmd->height = height;
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000616
Jiawei Shaob47470d2019-03-05 01:02:47 +0000617 return new RenderPassEncoderBase(device, this, &mAllocator);
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000618 }
619
620 void CommandEncoderBase::CopyBufferToBuffer(BufferBase* source,
621 uint32_t sourceOffset,
622 BufferBase* destination,
623 uint32_t destinationOffset,
624 uint32_t size) {
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000625 if (ConsumedError(ValidateCanRecordTopLevelCommands())) {
626 return;
627 }
628
Jiawei Shaod6eb2e72019-03-05 00:03:28 +0000629 if (ConsumedError(GetDevice()->ValidateObject(source))) {
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000630 return;
631 }
632
Jiawei Shaod6eb2e72019-03-05 00:03:28 +0000633 if (ConsumedError(GetDevice()->ValidateObject(destination))) {
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000634 return;
635 }
636
637 CopyBufferToBufferCmd* copy =
638 mAllocator.Allocate<CopyBufferToBufferCmd>(Command::CopyBufferToBuffer);
639 new (copy) CopyBufferToBufferCmd;
640 copy->source.buffer = source;
641 copy->source.offset = sourceOffset;
642 copy->destination.buffer = destination;
643 copy->destination.offset = destinationOffset;
644 copy->size = size;
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000645 }
646
647 void CommandEncoderBase::CopyBufferToTexture(const BufferCopyView* source,
648 const TextureCopyView* destination,
649 const Extent3D* copySize) {
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000650 if (ConsumedError(ValidateCanRecordTopLevelCommands())) {
651 return;
652 }
653
Jiawei Shaod6eb2e72019-03-05 00:03:28 +0000654 if (ConsumedError(GetDevice()->ValidateObject(source->buffer))) {
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000655 return;
656 }
657
Jiawei Shaod6eb2e72019-03-05 00:03:28 +0000658 if (ConsumedError(GetDevice()->ValidateObject(destination->texture))) {
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000659 return;
660 }
661
662 CopyBufferToTextureCmd* copy =
663 mAllocator.Allocate<CopyBufferToTextureCmd>(Command::CopyBufferToTexture);
664 new (copy) CopyBufferToTextureCmd;
665 copy->source.buffer = source->buffer;
666 copy->source.offset = source->offset;
667 copy->destination.texture = destination->texture;
668 copy->destination.origin = destination->origin;
669 copy->copySize = *copySize;
670 copy->destination.level = destination->level;
671 copy->destination.slice = destination->slice;
672 if (source->rowPitch == 0) {
673 copy->source.rowPitch = ComputeDefaultRowPitch(destination->texture, copySize->width);
674 } else {
675 copy->source.rowPitch = source->rowPitch;
676 }
677 if (source->imageHeight == 0) {
678 copy->source.imageHeight = copySize->height;
679 } else {
680 copy->source.imageHeight = source->imageHeight;
681 }
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000682 }
683
684 void CommandEncoderBase::CopyTextureToBuffer(const TextureCopyView* source,
685 const BufferCopyView* destination,
686 const Extent3D* copySize) {
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000687 if (ConsumedError(ValidateCanRecordTopLevelCommands())) {
688 return;
689 }
690
Jiawei Shaod6eb2e72019-03-05 00:03:28 +0000691 if (ConsumedError(GetDevice()->ValidateObject(source->texture))) {
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000692 return;
693 }
694
Jiawei Shaod6eb2e72019-03-05 00:03:28 +0000695 if (ConsumedError(GetDevice()->ValidateObject(destination->buffer))) {
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000696 return;
697 }
698
699 CopyTextureToBufferCmd* copy =
700 mAllocator.Allocate<CopyTextureToBufferCmd>(Command::CopyTextureToBuffer);
701 new (copy) CopyTextureToBufferCmd;
702 copy->source.texture = source->texture;
703 copy->source.origin = source->origin;
704 copy->copySize = *copySize;
705 copy->source.level = source->level;
706 copy->source.slice = source->slice;
707 copy->destination.buffer = destination->buffer;
708 copy->destination.offset = destination->offset;
709 if (destination->rowPitch == 0) {
710 copy->destination.rowPitch = ComputeDefaultRowPitch(source->texture, copySize->width);
711 } else {
712 copy->destination.rowPitch = destination->rowPitch;
713 }
714 if (destination->imageHeight == 0) {
715 copy->destination.imageHeight = copySize->height;
716 } else {
717 copy->destination.imageHeight = destination->imageHeight;
718 }
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000719 }
720
721 CommandBufferBase* CommandEncoderBase::Finish() {
722 if (GetDevice()->ConsumedError(ValidateFinish())) {
723 return CommandBufferBase::MakeError(GetDevice());
724 }
725 ASSERT(!IsError());
726
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000727 mEncodingState = EncodingState::Finished;
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000728
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000729 MoveToIterator();
730 return GetDevice()->CreateCommandBuffer(this);
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000731 }
732
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000733 // Implementation of functions to interact with sub-encoders
734
735 void CommandEncoderBase::HandleError(const char* message) {
736 if (!mGotError) {
737 mGotError = true;
738 mErrorMessage = message;
739 }
740 }
741
742 void CommandEncoderBase::ConsumeError(ErrorData* error) {
743 HandleError(error->GetMessage().c_str());
744 delete error;
745 }
746
747 void CommandEncoderBase::PassEnded() {
748 if (mEncodingState == EncodingState::ComputePass) {
749 mAllocator.Allocate<EndComputePassCmd>(Command::EndComputePass);
750 } else {
751 ASSERT(mEncodingState == EncodingState::RenderPass);
752 mAllocator.Allocate<EndRenderPassCmd>(Command::EndRenderPass);
753 }
754 mEncodingState = EncodingState::TopLevel;
755 }
756
757 // Implementation of the command buffer validation that can be precomputed before submit
758
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000759 MaybeError CommandEncoderBase::ValidateFinish() {
760 DAWN_TRY(GetDevice()->ValidateObject(this));
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000761
762 if (mGotError) {
763 return DAWN_VALIDATION_ERROR(mErrorMessage);
764 }
765
766 if (mEncodingState != EncodingState::TopLevel) {
767 return DAWN_VALIDATION_ERROR("Command buffer recording ended mid-pass");
768 }
769
770 MoveToIterator();
771 mIterator.Reset();
772
773 Command type;
774 while (mIterator.NextCommandId(&type)) {
775 switch (type) {
776 case Command::BeginComputePass: {
777 mIterator.NextCommand<BeginComputePassCmd>();
778 DAWN_TRY(ValidateComputePass());
779 } break;
780
781 case Command::BeginRenderPass: {
782 BeginRenderPassCmd* cmd = mIterator.NextCommand<BeginRenderPassCmd>();
783 DAWN_TRY(ValidateRenderPass(cmd));
784 } break;
785
786 case Command::CopyBufferToBuffer: {
787 CopyBufferToBufferCmd* copy = mIterator.NextCommand<CopyBufferToBufferCmd>();
788
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000789 DAWN_TRY(ValidateCopySizeFitsInBuffer(copy->source, copy->size));
790 DAWN_TRY(ValidateCopySizeFitsInBuffer(copy->destination, copy->size));
Yan, Shaobo738567f2019-02-27 02:46:27 +0000791 DAWN_TRY(ValidateB2BCopySizeAlignment(copy->size, copy->source.offset,
792 copy->destination.offset));
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000793
794 DAWN_TRY(ValidateCanUseAs(copy->source.buffer.Get(),
795 dawn::BufferUsageBit::TransferSrc));
796 DAWN_TRY(ValidateCanUseAs(copy->destination.buffer.Get(),
797 dawn::BufferUsageBit::TransferDst));
798
799 mResourceUsages.topLevelBuffers.insert(copy->source.buffer.Get());
800 mResourceUsages.topLevelBuffers.insert(copy->destination.buffer.Get());
801 } break;
802
803 case Command::CopyBufferToTexture: {
804 CopyBufferToTextureCmd* copy = mIterator.NextCommand<CopyBufferToTextureCmd>();
805
Jiawei Shao081d5c22019-03-04 12:01:59 +0000806 DAWN_TRY(
807 ValidateTextureSampleCountInCopyCommands(copy->destination.texture.Get()));
808
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000809 uint32_t bufferCopySize = 0;
810 DAWN_TRY(ValidateRowPitch(copy->destination.texture->GetFormat(),
811 copy->copySize, copy->source.rowPitch));
812
813 DAWN_TRY(ComputeTextureCopyBufferSize(copy->copySize, copy->source.rowPitch,
814 copy->source.imageHeight,
815 &bufferCopySize));
816
817 DAWN_TRY(ValidateCopySizeFitsInTexture(copy->destination, copy->copySize));
818 DAWN_TRY(ValidateCopySizeFitsInBuffer(copy->source, bufferCopySize));
819 DAWN_TRY(
820 ValidateTexelBufferOffset(copy->destination.texture.Get(), copy->source));
821
822 DAWN_TRY(ValidateCanUseAs(copy->source.buffer.Get(),
823 dawn::BufferUsageBit::TransferSrc));
824 DAWN_TRY(ValidateCanUseAs(copy->destination.texture.Get(),
825 dawn::TextureUsageBit::TransferDst));
826
827 mResourceUsages.topLevelBuffers.insert(copy->source.buffer.Get());
828 mResourceUsages.topLevelTextures.insert(copy->destination.texture.Get());
829 } break;
830
831 case Command::CopyTextureToBuffer: {
832 CopyTextureToBufferCmd* copy = mIterator.NextCommand<CopyTextureToBufferCmd>();
833
Jiawei Shao081d5c22019-03-04 12:01:59 +0000834 DAWN_TRY(ValidateTextureSampleCountInCopyCommands(copy->source.texture.Get()));
835
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000836 uint32_t bufferCopySize = 0;
837 DAWN_TRY(ValidateRowPitch(copy->source.texture->GetFormat(), copy->copySize,
838 copy->destination.rowPitch));
839 DAWN_TRY(ComputeTextureCopyBufferSize(
840 copy->copySize, copy->destination.rowPitch, copy->destination.imageHeight,
841 &bufferCopySize));
842
843 DAWN_TRY(ValidateCopySizeFitsInTexture(copy->source, copy->copySize));
844 DAWN_TRY(ValidateCopySizeFitsInBuffer(copy->destination, bufferCopySize));
845 DAWN_TRY(
846 ValidateTexelBufferOffset(copy->source.texture.Get(), copy->destination));
847
848 DAWN_TRY(ValidateCanUseAs(copy->source.texture.Get(),
849 dawn::TextureUsageBit::TransferSrc));
850 DAWN_TRY(ValidateCanUseAs(copy->destination.buffer.Get(),
851 dawn::BufferUsageBit::TransferDst));
852
853 mResourceUsages.topLevelTextures.insert(copy->source.texture.Get());
854 mResourceUsages.topLevelBuffers.insert(copy->destination.buffer.Get());
855 } break;
856
857 default:
858 return DAWN_VALIDATION_ERROR("Command disallowed outside of a pass");
859 }
860 }
861
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000862 return {};
863 }
864
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000865 MaybeError CommandEncoderBase::ValidateComputePass() {
866 PassResourceUsageTracker usageTracker;
867 CommandBufferStateTracker persistentState;
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000868
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000869 Command type;
870 while (mIterator.NextCommandId(&type)) {
871 switch (type) {
872 case Command::EndComputePass: {
873 mIterator.NextCommand<EndComputePassCmd>();
874
Brandon Jones11d32c82019-02-20 20:21:00 +0000875 DAWN_TRY(ValidateDebugGroups(mDebugGroupStackSize));
876
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000877 DAWN_TRY(usageTracker.ValidateUsages(PassType::Compute));
878 mResourceUsages.perPass.push_back(usageTracker.AcquireResourceUsage());
879 return {};
880 } break;
881
882 case Command::Dispatch: {
883 mIterator.NextCommand<DispatchCmd>();
884 DAWN_TRY(persistentState.ValidateCanDispatch());
885 } break;
886
Brandon Jones11d32c82019-02-20 20:21:00 +0000887 case Command::InsertDebugMarker: {
888 InsertDebugMarkerCmd* cmd = mIterator.NextCommand<InsertDebugMarkerCmd>();
889 mIterator.NextData<char>(cmd->length + 1);
890 } break;
891
892 case Command::PopDebugGroup: {
893 mIterator.NextCommand<PopDebugGroupCmd>();
894 DAWN_TRY(PopDebugMarkerStack(&mDebugGroupStackSize));
895 } break;
896
897 case Command::PushDebugGroup: {
898 PushDebugGroupCmd* cmd = mIterator.NextCommand<PushDebugGroupCmd>();
899 mIterator.NextData<char>(cmd->length + 1);
900 DAWN_TRY(PushDebugMarkerStack(&mDebugGroupStackSize));
901 } break;
902
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000903 case Command::SetComputePipeline: {
904 SetComputePipelineCmd* cmd = mIterator.NextCommand<SetComputePipelineCmd>();
905 ComputePipelineBase* pipeline = cmd->pipeline.Get();
906 persistentState.SetComputePipeline(pipeline);
907 } break;
908
909 case Command::SetPushConstants: {
910 SetPushConstantsCmd* cmd = mIterator.NextCommand<SetPushConstantsCmd>();
911 mIterator.NextData<uint32_t>(cmd->count);
912 // Validation of count and offset has already been done when the command was
913 // recorded because it impacts the size of an allocation in the
914 // CommandAllocator.
915 if (cmd->stages & ~dawn::ShaderStageBit::Compute) {
916 return DAWN_VALIDATION_ERROR(
917 "SetPushConstants stage must be compute or 0 in compute passes");
918 }
919 } break;
920
921 case Command::SetBindGroup: {
922 SetBindGroupCmd* cmd = mIterator.NextCommand<SetBindGroupCmd>();
923
924 TrackBindGroupResourceUsage(cmd->group.Get(), &usageTracker);
925 persistentState.SetBindGroup(cmd->index, cmd->group.Get());
926 } break;
927
928 default:
929 return DAWN_VALIDATION_ERROR("Command disallowed inside a compute pass");
930 }
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000931 }
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000932
933 UNREACHABLE();
934 return DAWN_VALIDATION_ERROR("Unfinished compute pass");
935 }
936
937 MaybeError CommandEncoderBase::ValidateRenderPass(BeginRenderPassCmd* renderPass) {
938 PassResourceUsageTracker usageTracker;
939 CommandBufferStateTracker persistentState;
940
941 // Track usage of the render pass attachments
942 for (uint32_t i : IterateBitSet(renderPass->colorAttachmentsSet)) {
943 RenderPassColorAttachmentInfo* colorAttachment = &renderPass->colorAttachments[i];
944 TextureBase* texture = colorAttachment->view->GetTexture();
945 usageTracker.TextureUsedAs(texture, dawn::TextureUsageBit::OutputAttachment);
Jiawei Shao1e1c13e2019-03-11 18:41:02 +0000946
947 TextureViewBase* resolveTarget = colorAttachment->resolveTarget.Get();
948 if (resolveTarget != nullptr) {
949 usageTracker.TextureUsedAs(resolveTarget->GetTexture(),
950 dawn::TextureUsageBit::OutputAttachment);
951 }
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000952 }
953
954 if (renderPass->hasDepthStencilAttachment) {
955 TextureBase* texture = renderPass->depthStencilAttachment.view->GetTexture();
956 usageTracker.TextureUsedAs(texture, dawn::TextureUsageBit::OutputAttachment);
957 }
958
959 Command type;
960 while (mIterator.NextCommandId(&type)) {
961 switch (type) {
962 case Command::EndRenderPass: {
963 mIterator.NextCommand<EndRenderPassCmd>();
964
Brandon Jones11d32c82019-02-20 20:21:00 +0000965 DAWN_TRY(ValidateDebugGroups(mDebugGroupStackSize));
966
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000967 DAWN_TRY(usageTracker.ValidateUsages(PassType::Render));
968 mResourceUsages.perPass.push_back(usageTracker.AcquireResourceUsage());
969 return {};
970 } break;
971
972 case Command::Draw: {
973 mIterator.NextCommand<DrawCmd>();
974 DAWN_TRY(persistentState.ValidateCanDraw());
975 } break;
976
977 case Command::DrawIndexed: {
978 mIterator.NextCommand<DrawIndexedCmd>();
979 DAWN_TRY(persistentState.ValidateCanDrawIndexed());
980 } break;
981
Brandon Jones11d32c82019-02-20 20:21:00 +0000982 case Command::InsertDebugMarker: {
983 InsertDebugMarkerCmd* cmd = mIterator.NextCommand<InsertDebugMarkerCmd>();
984 mIterator.NextData<char>(cmd->length + 1);
985 } break;
986
987 case Command::PopDebugGroup: {
988 mIterator.NextCommand<PopDebugGroupCmd>();
989 DAWN_TRY(PopDebugMarkerStack(&mDebugGroupStackSize));
990 } break;
991
992 case Command::PushDebugGroup: {
993 PushDebugGroupCmd* cmd = mIterator.NextCommand<PushDebugGroupCmd>();
994 mIterator.NextData<char>(cmd->length + 1);
995 DAWN_TRY(PushDebugMarkerStack(&mDebugGroupStackSize));
996 } break;
997
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000998 case Command::SetRenderPipeline: {
999 SetRenderPipelineCmd* cmd = mIterator.NextCommand<SetRenderPipelineCmd>();
1000 RenderPipelineBase* pipeline = cmd->pipeline.Get();
1001
1002 if (!pipeline->IsCompatibleWith(renderPass)) {
1003 return DAWN_VALIDATION_ERROR(
1004 "Pipeline is incompatible with this render pass");
1005 }
1006
1007 persistentState.SetRenderPipeline(pipeline);
1008 } break;
1009
1010 case Command::SetPushConstants: {
1011 SetPushConstantsCmd* cmd = mIterator.NextCommand<SetPushConstantsCmd>();
1012 mIterator.NextData<uint32_t>(cmd->count);
1013 // Validation of count and offset has already been done when the command was
1014 // recorded because it impacts the size of an allocation in the
1015 // CommandAllocator.
1016 if (cmd->stages &
1017 ~(dawn::ShaderStageBit::Vertex | dawn::ShaderStageBit::Fragment)) {
1018 return DAWN_VALIDATION_ERROR(
1019 "SetPushConstants stage must be a subset of (vertex|fragment) in "
1020 "render passes");
1021 }
1022 } break;
1023
1024 case Command::SetStencilReference: {
1025 mIterator.NextCommand<SetStencilReferenceCmd>();
1026 } break;
1027
1028 case Command::SetBlendColor: {
1029 mIterator.NextCommand<SetBlendColorCmd>();
1030 } break;
1031
1032 case Command::SetScissorRect: {
1033 mIterator.NextCommand<SetScissorRectCmd>();
1034 } break;
1035
1036 case Command::SetBindGroup: {
1037 SetBindGroupCmd* cmd = mIterator.NextCommand<SetBindGroupCmd>();
1038
1039 TrackBindGroupResourceUsage(cmd->group.Get(), &usageTracker);
1040 persistentState.SetBindGroup(cmd->index, cmd->group.Get());
1041 } break;
1042
1043 case Command::SetIndexBuffer: {
1044 SetIndexBufferCmd* cmd = mIterator.NextCommand<SetIndexBufferCmd>();
1045
1046 usageTracker.BufferUsedAs(cmd->buffer.Get(), dawn::BufferUsageBit::Index);
1047 persistentState.SetIndexBuffer();
1048 } break;
1049
1050 case Command::SetVertexBuffers: {
1051 SetVertexBuffersCmd* cmd = mIterator.NextCommand<SetVertexBuffersCmd>();
1052 auto buffers = mIterator.NextData<Ref<BufferBase>>(cmd->count);
1053 mIterator.NextData<uint32_t>(cmd->count);
1054
1055 for (uint32_t i = 0; i < cmd->count; ++i) {
1056 usageTracker.BufferUsedAs(buffers[i].Get(), dawn::BufferUsageBit::Vertex);
1057 }
1058 persistentState.SetVertexBuffer(cmd->startSlot, cmd->count);
1059 } break;
1060
1061 default:
1062 return DAWN_VALIDATION_ERROR("Command disallowed inside a render pass");
1063 }
1064 }
1065
1066 UNREACHABLE();
1067 return DAWN_VALIDATION_ERROR("Unfinished render pass");
1068 }
1069
1070 MaybeError CommandEncoderBase::ValidateCanRecordTopLevelCommands() const {
1071 if (mEncodingState != EncodingState::TopLevel) {
1072 return DAWN_VALIDATION_ERROR("Command cannot be recorded inside a pass");
1073 }
1074 return {};
Corentin Walleze1f0d4e2019-02-15 12:54:08 +00001075 }
1076
1077} // namespace dawn_native